In 2010 (we were still using Net 2.0 at the time) we encountered a problem that mail were not send to Bcc recipients when using the SmtpDeliveryMethod.SpecifiedPickupDirectory with the SmtpClient.
After searching the web (link and link), i made the following workaround:
if (message.Bcc.Count > 0)
{
System.Text.StringBuilder bcc = new System.Text.StringBuilder();
for (int i = 0; i < message.Bcc.Count; i++)
{
bcc.Append(message.Bcc[i].Address);
bcc.Append(";");
}
message.Headers.Add("Bcc", bcc.ToString());
message.Bcc.Clear();
}
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.SpecifiedPickupDirectory;
smtp.PickupDirectoryLocation = PickupDir;
smtp.Send(message);
This workaround had as side-effect that the Bcc recipient was visible in the header of the send mail, but in our case this wasn't an issue as Bcc was used for logging purposes.
A a few days ago we switched to Net 4.0 (finally) and the problem resurfaced. I tried removing the workaround and that didn't help either.
Switching to SmtpDeliveryMethod.Network isn't a solution as the send method is blocking and keeping the user waiting (depending on the mail server load) while a mail is send is not an option. Sending the mail async isn't isn't an option too, as the mail is lost when the send fails or the process crashes.
I can't seem to find any solution on the web for this problem, so does anyone have any ideas ?
This doesn't work in .Net 4.0. But it is not a Bug it is designed that way. Take a look at this link
As it seems there is a Bug in .Net 3.5 that allows to implement the behavior you're looking for.
Related
I am trying to send a standard email message through AWS WorkMail using the SmtpClient in .NET Core. The configuration is very standard according to Amazon Documentation:
https://docs.amazonaws.cn/en_us/general/latest/gr/workmail.html
"Smtp": {
"MailServer": "smtp.mail.eu-west-1.awsapps.com",
"MailPort": "465",
"SenderName": "Us us us",
"FromAddress": "email#domain.com",
"Username": "email#domain.com",
"Password": "Password1$",
"EnableSsl": "true"
},
The settings are injected and the SmtpClient gets properly instantiated and the sending of the email is just:
var mail = new MailMessage
{
From = new MailAddress(_smtpSettings.FromAddress, _smtpSettings.SenderName),
SubjectEncoding = Encoding.UTF8,
BodyEncoding = Encoding.UTF8,
IsBodyHtml = true,
Body = message,
Subject = subject,
Priority = MailPriority.High
};
mail.To.Add(new MailAddress(sendToEmail));
_smtpClient.Send(mail);
Sadly, the sending always fails with a gateway timeout. We tried switching for 587 for STARTTLS and providing the Username without the domain (#). The server sending has proper SSL certificate installed and the mailserver is exactly the one we have our smtp on. Although this would not be a relevant solution, I also increased the timeout to 10 seconds (even though this would not be a solution).
Any idea what could be wrong? I am tearing my hair already. Seems to be an issue with AWS WorkMail.
Just adding to the existing comment. SMTPClient is actually obsolete/deprecated but is not being marked as so. See comments here (https://github.com/dotnet/dotnet-api-docs/issues/2986#issuecomment-430805681)
Essentially it boils down to SmtpClient hasn't been updated in years and is missing many features. The .NET Core team wanted to mark it as obsolete, but some developers have existing projects with "Warnings as Errors" turned on. It would instantly make any project that is using SmtpClient with Warnings as Errors turned on suddenly stop building. So... It's kinda deprecated, but not being marked so in some official docs.
MailKit is actually being pushed by Microsoft full stop for people to use when it comes to Email. Much to some developers chargrin who don't want to use a third party library for such a "simple" and common feature. Just in my personal experience, I think Mailkit is great and super easy to use. A quick guide to getting up and running is here : https://dotnetcoretutorials.com/2017/11/02/using-mailkit-send-receive-email-asp-net-core/
So the problem is that the default .NET Core SmtpClient does not support StartSSL (implicit SSL) which is the only accepted option by WorkMail. You see, WorkMail allows only connections that start from SSL and the SmtpClient first starts from unencrypted and then switches over to encrypted if it cannot connect.
If you are trying this, you will not get this to work using standard SmtpClient and as usually the case with Microsoft, they don't recognize it as an issue. You can either try tunneling or better just use one of the available libraries. Sadly, most of them are paid, there is AIM but it doesn't work with .NET Core and I didn't want to spend time porting not my own library to .NET Standard so I ended up using Mailkit.
There are some issues with the library though, first, before sending you have to call Connect which takes host and port as parameter, that means you cannot just inject premade smtpclient as singleton and have to instantiate it within the place of usage. That's dope and no interface also makes it unmockable which might blow your integration tests. Moreover, you have to do an ugly line before calling send like so:
emailClient.AuthenticationMechanisms.Remove("XOAUTH2");
But at least it works.
I have a server that runs IIS and PHP. I have numerous webpages that send emails, some to me, some to the users. This all works great. I am on a Comcast Business Class account which means I can use smtp.comcast.net as my SMTP server, use port 25, and not use any sort of authentication which is great. And it works just fine.
Now fast forward to today. I am writing some custom C# code to monitor a folder structure and basically email me the new file if it matches certain parameters. In my C# code, I try to use the same settings, but it doesn't work. The SmtpClient.Send() function does not throw an Exception and my code completes the routine as if everything is happy and working. But then I wait and wait and wait, and I never receive the email.
SmtpClient smtp = new SmtpClient("smtp.comcast.net");
smtp.Port = 25;
smtp.EnableSsl = false;
smtp.UseDefaultCredentials = false;
smtp.Timeout = 2500;
smtp.Send(mail);
onStatusUpdate("Successfully sent email to " + mail.To + (mail.CC.Count > 0 ? " and CC'd " + mail.CC.ToString() : ""));
The "mail" object is of type MailMessage and is setup with the To, From, subject, body, and CC. Also has HTML and Plain Text alternate views.
I guess the easiest question, is if there is a trick to sending email the oldschool port-25 way in C# that doesn't exist in PHP?
And the only reason I mention PHP is because I know my firewall isn't blocking port 25, I know my ISP has it open, I know I have the right server, I know it should work.
I don't know if this affects their Business Class accounts, but Comcast just recently (<2 months ago) closed off port 25 for all their email accounts. try using port 587
http://customer.comcast.com/help-and-support/internet/email-client-programs-with-xfinity-email/
The first place to look is in the mail server logs of the outgoing mail server that you are using to send this message. These should tell you whether or not the mail server is even receiving the message from your C# program for queuing, and if so - what's happening when it attempts to deliver the message to the remote MTA.
Well about 5 hours after I started testing, I got all my test emails at once including the embedded HTML and everything else. So it is working just as it should. I guess since the signature was slightly different they block them until they are deemed non-spam. It also appears that now when I send an email it goes through instantly.
So Comcast has some sort of time delay filter apparently for anybody else in the future with this problem.
I ended up adding mails to the (to list),
and put sending in foreach for every mail I send mail separately,
and it worked!
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.
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
I'd like to write a service that periodically checks a POP3 account for new messages and based on custom business logic forwards the messages to an appropriate "To", and possibly changes the "From" as well. I might need to keep some messages on the server until certain conditions are ready for them to be forwarded.
I found a sample using Chilkat .NET components that might work:
http://www.example-code.com/csharp/pop3_forwarder.asp
My question is: Are there any other examples of this in the .NET space using any other components?
Thanks!
The following SO questions/answers might help finding components for the POP3 part of your porject:
Reading Email using Pop3 in C#
Free POP3 .NET library?
And you can use SmtpClient in System.Net.Mail for sending the mails:
Sending E-mail using C#
I implemented something very similar using MailBee's IMAP, POP and SMTP .NET components.
They're not free, I'm afraid, but I've found them to be pretty solid, and AfterLogic's support is fast.
There's also the free (including source code) LumiSoft Mail Server, that has POP3 relay support to collect messages from a POP3 server and manage them from there, you could adapt that? (It's written in C#, is nice to work with and upgrades cleanly to VS2008). I've had no problems with that either.
Try Mail.dll .NET email component. It has SSL support, POP3 and SMTP clients.
using(Pop3 pop3 = new Pop3())
{
pop3.Connect("mail.host.com"); // Connect to the server
pop3.Login("user", "password");
foreach(string uid in pop3.GetAll())
{
// Receive mail
IMail mail = new MailBuilder()
.CreateFromEml(pop3.GetMessageByUID(uid));
Console.WriteLine(mail.Subject);
}
pop3.Close(true);
}
You can download it here