bcd
bcd

Reputation: 155

Sending email through MS Outlook and disabling the warning

I have a C# program that I will be running on a daily basis (through Windows Scheduler). The program is to send a daily report to my team. I have written the following to send the email and it works. the only problem is that Outlook shows a message box " A program is trying to send an e-mail message on your behalf. if this is unexpected...... " . there are three buttons "allow" "deny" "help" and it seems like my program is halted at that point and until i click the allow or deny button , the program doesn't send the email. I know that the i can change the options by going into tools -> trust center -> programmatic access, but i would really like to not use that because this program would be eventually running from another machine where the user may or may not access to change the setting in trust center. Is there a way to disable this warning programatically? ..or is there another way to send the email without having this warning popup

here is the code used to send the email..and it works fine..

Application olook = new Application();
        NameSpace ns = olook.GetNamespace("MAPI");
        ns.Logon(null, null, true, true);

       _MailItem msg = (_MailItem)olook.CreateItem(OlItemType.olMailItem);

        msg.To = "[email protected]";
        msg.Subject = "test";

        msg.HTMLBody = strHTML;

        msg.Send();
        ns.Logoff();

Upvotes: 4

Views: 8007

Answers (2)

jrummell
jrummell

Reputation: 43087

One of the easiest solutions is to use Exchange's SMTP server. Here's an example from MSDN.

string to = "[email protected]";
string from = "[email protected]";
MailMessage message = new MailMessage(from, to);
message.Subject = "Using the new SMTP client.";
message.Body = @"Using this new feature, you can send an e-mail message from an application very easily.";

SmtpClient client = new SmtpClient(server);
// Credentials are necessary if the server requires the client 
// to authenticate before it will send e-mail on the client's behalf.
client.UseDefaultCredentials = true;
client.Send(message);

Of course, you'll have to check with your Exchange administrator to make sure that SMTP is enabled.

Upvotes: 1

stylefish
stylefish

Reputation: 571

there are several ways to do that you could disable the popup like @DJ KRAZE described

or you could send a message via smtp, if thats possible in your environment see this: http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx

then you could use the "redemption library" i've used it and there will be no messages, because redemption suppresses them (or works around them) but the library is used via com, thats not that comfortable.. although you have to pay for that: http://www.dimastr.com/redemption/home.htm

the thir alternative is using the managed Exchange Web Services http://www.microsoft.com/download/en/details.aspx?id=13480 this is pretty straight forward and fun to use. you can get that via NuGet as well. :)

EDIT:

i forgot to mention, that Exchange Web Services are only available on Exchange 2007 SP1 or higher. and this is what it looks like to send a message (after connect to the server)

EmailMessage message = new EmailMessage(service);
message.Subject = "Hello from the EWS Managed API";
message.Body = "Now that's easy!";
message.ToRecipients.Add("[email protected]");
message.Save();

look here for an introduction: http://msdn.microsoft.com/en-us/library/dd637749(v=exchg.80).aspx

Upvotes: 1

Related Questions