Sending email through MS Outlook and disabling the warning - c#

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 = "xxx#xxx.com";
msg.Subject = "test";
msg.HTMLBody = strHTML;
msg.Send();
ns.Logoff();

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("someone#fabrikam.com");
message.Save();
look here for an introduction: http://msdn.microsoft.com/en-us/library/dd637749(v=exchg.80).aspx

One of the easiest solutions is to use Exchange's SMTP server. Here's an example from MSDN.
string to = "jane#contoso.com";
string from = "ben#contoso.com";
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.

Related

net_io_connectionclosed error trying to send via office365 in c#

I have looked at the other threads and have so far found no solution.
I get the net_io_connectionclosed error whenever I try to connect via the method below.
I am trying to use an Office 365 smtp relay. I do not have access to the main account or ability to change anything about this email so I cannot check settings or even reset the password.
Any and all help gratefully received.
uname: email address of the relay account (no-reply#[x].com)
I am waiting on a reply to my email to get the onmicrosoft.com address, but it is not mentioned in any of the tutorials at all. As such the uname here is also in the msg as the 'from' address.
pwd: password.
SmtpClient client = new SmtpClient("smtp.office365.com", 587);
client.UseDefaultCredentials = false;
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential(uname, pwd);
client.TargetName = "STARTTLS/smtp.office365.com";
client.Timeout = 10000;
client.Send(msg);
Ok, it turns out that using a relay for this is not possible unless you own the IPs (not supplied by Azure, netregistry etc.)
You have to buy a new mailbox from O365 and use that. Thus defeating the purpose of having a realy unless you run your own server (in which case I wouldn't need a relay as I would use my own smtp services.)
This means if you are running Azure, probably best to avoid O365 for email, other suppliers are far more flexible.
Link to the forum at MS:
https://answers.microsoft.com/en-us/msoffice/forum/msoffice_o365admin-mso_other/how-to-setup-an-smtp-relay-for-use-from-an-azure/5d997c38-bd67-4dbd-ad0d-62488addf9ae

Sending email via exchange server issue. (system.net.mail vs microsoft.exchange.webservices)

I am developing a web application for my company. Part of it requires the internal user to fill out a form, and send an email on this user's behalf. It is all within the company. I searched and found two possible routes old system.net.mail and a more recent microsoft.exchange.webservices, but seems our exchange server requires credentials. I can only get the user's login and his email address login+"#company.com". How can i get this done?
Below are the codes i used smtp (system.net.mail), but it doesnt work.
string[] username =System.Security.Principal.WindowsIdentity.GetCurrent().Name.Split('\\');
string email = username[1] + "#company.com";
MailAddress from = new MailAddress(email);
MailAddress to = new MailAddress("someone#company.com");
MailMessage message = new MailMessage(from, to);
message.Subject = "testmail";
message.Body = "<h>testmail</h>";
message.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(message);
SmtpClient requires that you either specify the SMTP server directly, or that you have the right application/machine configuration settings for it to detect the server automatically. Your code is choosing the second option implicitly, and I suspect you don't have the right settings in app/machine config. Exchange does NOT support SMTP in its default configuration, afaik, so unless someone familiar with your Exchange server knows SMTP is configured and can give you the right address, SmtpClient is probably out.
Exchange Web Services (aka EWS) is probably your better answer, but it's not really a good one. In order to use exchange web services, you will need one of:
1) The domain, username, and password of the user so that you can pass the right NetworkCredential to EWS. In your case, this would probably mean the user has to enter their password into your form, which may break your requirements.
2) The user that the process is running as (in a web application, the application pool identity for IIS) has to have permissions to send mail as the user in question.
3) If you can use ASP.NET authentication to impersonate the user (this would only be a good approach in a LAN application), then you can effectively fall back to option (2), because now you will be talking to EWS as the user, who obviously will have permission to send mail from their own address.
As you can see, the right approach depends greatly on your Exchange/Active Directory/LAN setup.

Not able to connect to SMTP port of server

I have following piece of code, usiong which I am trying to send an email. I am using another server as SMPTClient.
MailMessage message = new MailMessage();
message.To.Add(toEmailId);
message.Subject = "test Subject";
message.From = new System.Net.Mail.MailAddress("myid#xyz.com");
message.Body = "This is a system generated email. Please do not reply";
SmtpClient smtp = new SmtpClient("anotherservername");
smtp.Send(message);
While debuggin, I get error at last line. The error is:
An attempt was made to access a socket in a way forbidden by its access permissions SERVER IP :25
I found out from internet that My firewall might be blocking me to access that server's port, or is there any setting, configuration which I have missed. Apparently the applications deployed on that server are able to send emails. I am still building one to test.
Some servers perform authentication and authorization by checking if you've connected to POP server some time before connecting to SMTP. You should try first connecting and authenticating with POP and afterwards connecting to SMTP.
Historically, email clients first checked and then sent emails in Send&Receive routines, so this was used as poor mans authorization. Take a look at wikipedia article about POP before SMTP.
Another issue which caused this for some people is how server address is provided to SmtpClient. You should use constructor which explicitly provides port number instead of appending it to IP/hostname. I.e.
instead of new SmtpClient("123.123.123.123:25")
use new SmtpClient("123.123.123.123", 25)

Sending email using outlook express in asp.net c#

I would like to know how to open Outlook Express mail client for mailing through web application in Asp.Net?
I mean to say, can we call Outlook Express to send mail through web application?
For example, when there is need to send mail, I will click on a button which will open Outlook Express's New Message window. Now my message should go through Outlook Express. I will be making use of Outlook Express Address Book to store my email contacts. Now if I get any mail it will come in Outlook Express but popup message should come in my Web application that "you have an email message pending" like that somthing.
Waiting for the reply.....please
To answer you first question: Yes it is possible to send an e-mail from a web application. Try this (from the client side if you are using Silverlight):
HtmlPage.Window.Navigate(new Uri("mailto:somemailaddress#gmail.com", UriKind.Absolute));
Or just have a mailto link (in HTML):
- http://webdesign.about.com/od/beginningtutorials/a/aabegin100299.htm
However, implementing a web service to send the mail is probably better. Try these (they are for Silverlight, but you'll get the idea):
- http://deepumi.wordpress.com/tag/send-email-from-silverlight/
- http://www.michaelsnow.com/2010/06/10/silverlight-tip-of-the-day-30-sending-email-from-silverlight/
If your company does not allow you access to an SMTP server you can use Google as one. Just create a gmail account and set up the server like this (with your gmail account name and password). I think they limit the amount of mails sent out to 100 per day.
_mailClient = new SmtpClient();
_mailClient.Host = "smtp.gmail.com";
_mailClient.Port = 587;
_mailClient.EnableSsl = true;
_mailClient.UseDefaultCredentials = false;
_mailClient.Credentials = new NetworkCredential(username, password);
_mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
Take a look at this post. You can even edit the HTML message and then, through Javascript, 'navigate' to an anchor tag that points to a mailto: location. As for the pop-ups, you need to integrate either the navigator or the webpage with the POP/IMAP/whatever mailserver where the message will be stored/retrieved/received.

What could cause a message sent from Gmail SMTP using C# not to arrive - No exception is thrown

I am trying to send mail through my Google Apps email account. This is setup on my own domain and is all working fine through the web interface and through outlook.
However, i'm trying to send an email from a webpage using C#, I get no exceptions and everything appears to go smoothly, but the emails never seem to arrive:
MailMessage msg = new MailMessage("no-reply#xxxx.co.uk", "dan#xxxx.co.uk");
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential("no-reply#xxxx.co.uk", "xxxxxxxxx");
smtp.EnableSsl = true;
msg.IsBodyHtml = true;
msg.Body = body;
smtp.Send(msg);
The body is a string which i've generated earlier in the code. As far as i'm aware, if there was a problem with actually connecting to the SMTP server at Gmail then I would get an exception thrown.
Any ideas what could cause this not to work? Only thing different I can see compared to other peoples examples is that I have set body as HTML and i'm sending from a Google Apps account rather than an #gmail.com account.
The google apps itself is all configured fully and working, i've set this up numerous times so I know it's not likely to be a problem there.
I also tried sending on port 25, as that's what you use when configuring Outlook to send from a Gmail account.
Same result for both, no exception thrown, but email never arrives. Both emails the sender and receiver are on my domain using Google Apps for Gmail.
EDIT:
Also I should mention that I have signed in using both accounts, and both have IMAP and POP enabled in their settings etc.
New findings
This is a strange one
If I send mail manually from:
no-reply#mydomain.com to dan#mydomain.com - Then it works
But if I send it through code this way...it doesn't work...
If I send the following through code it does work:
no-reply#mydomain.com to me#gmail.com or me#ntlworld.com - This works through code!
I would then be led to think that this means a problem with dan#mydomain.com receiving messages...But it receives any messages sent manually from any google, hotmail, or ntlworld email address i've tried.
So either Google Apps accounts can't receive email sent through code (unlikely) or something else is at play here
The server may silently discard your message if there is a problem with it.
- Despite any spec saying otherwise.
The message might be lost in the ether despite being "delivered".
The SMTP server may be applying severe filtering and might additionally require that your sender and destination email addresses match up 'correctly'
I suggest trying with a different SMTP host just to check. :)
When you setup Google Apps, even if the MX records are all working correctly for sending mail manually to/from other accounts, there can still be problems.
The answer is to wait 12-24 hours after setting up your MX Records, even if everything else is working fine.
If you receive no exception, then to me this is the only answer at the moment.
All is working correctly now. Even though all MX records were correct on my DNS and email appeared to be working, there were still changes going on in the background

Categories

Resources