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.
Related
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!
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.
I've been following this site with a lot of admiration especially on how questions are professionally answered so I decided to be fully involved.
Please, I need urgent help on a project that I have been working on for a long time but it's almost stalled now just because of a critical issue.
An aspect of the program automates email sending to clients using the free email server systems. Due to the high frequency of email sending, I observed that the email server we're sending to drops larger parts of the emails sent out and literally blocks delivery of major emails to the recipients.
I have tried to reduce the rate of sending email out but to no avail. My fear now is my IP address might have been blocked or may be blocked soon if this continue. The program is not spamming but have to be developed in order to contact a large database of recipients at a goal within short time - like about 1000 or more recipients.
I am using Webbrowser control in C# to automate the process of logging in to the mail server and sending the email out.
Now, what I want is a sample code to use publicly available web proxy servers for each email sent out such that the source IP address appears dynamic and different to the target email server each time a message is sent out to it.
I mean, I want to dynamically get and use free public proxy servers with the Webbrowser control to send out the emails. In this way I believe the email servers would not be able to reject the emails base on the IP address source. I want to know how to dynamically get literally one web proxy server for each email sent, if possible each time.
This project is very critical and this feature is a determinant. I have googled endlessly without any straight forward solution to this issue. I would, therefore, appreciate any useful help, sample codes or resources that could help me to solve this nagging problem once and for all.
Thank you!
Your problem is "free email server systems": they consider you a spammer, and the idea you suggest (spoofing IPs) will, if detected, ruin your reputation.
If you explain what you are trying to accomplish, perhaps someone here can offer a better design.
Are you trying to give people with free email accounts (like Hotmail) bulk-emailing capabilities?
First of all (if I understood your answer right), you don't have to use WebBrowser control - you can use specified .NET solutions that allows you to efficiently sending mails:
MailMessage msg = new MailMessage("from", "to", "subject", "body text");
SmtpClient client = new SmtpClient("smtp server");
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("user", "password");
client.UseDefaultCredentials = false;
client.Credentials = cred;
Client.Send(msg);
Unfortunately, if you want to send e-mails to many recipients and you want to be sure, that these messages reach the recipients - you have to do it using your own e-mail server or do it by purchase the service on a paid e-mail servers - then they will not treat you as a spammer.
But if you anyway want to send e-mails by rotation proxy servers or similar sollution - you can define your proxy:
SmtpClient client = new SmtpClient("my.proxy_server.com", 8080);
First you have to collect any list of available proxy servers which allows you to do it in reasonable time (servers switching can significantly increase total process time because conection time can be different for each proxy server)
Proxy servers list ordered by access time:
http://www.publicproxyservers.com/proxy/list_avr_time1.html
I am using
asp.net mvc 2.0
C#
.NET 4.0
ms sql server 2005
iis 7.0
I want to make an email notifier system just like many sites have such as google. I want user to be able to set a reminder date and when that date is hit a email is sent to them.
I am not sure how to do this.
I heard of a couple ways but have not found any tutorials on how to do them.
Windows scheduler
through ms sql server (think sql server agent?)
With the windows scheduler I don't think it would work on a shared hosting environment. I would prefer if it did but if there is a big difference then I can live with out that ability.
I also want in the very near future to support SMS messages so the solution should be able to expand to work with that as well if possible.
This blog post presents a very effective (though somewhat 'hacky') solution to your problem that will work in a shared hosting environment. This is what Jeff used in StackOverflow to assign badges to users (I don't know if SO is still using it though).
For the code to actually send the email, you should look around the Internet since there are endless code examples on how to do that. One possible answer could be:
public void SendEmail()
{
MailMessage loMsg = new MailMessage();
loMsg.From = new MailAddress("from#domain.com");
loMsg.To.Add(new MailAddress("to#domain.com"));
loMsg.Subject = "Subject";
loMsg.Body = "Email Body";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new System.Net.NetworkCredential("username", "password")
};
smtp.Send(loMsg);
}
Take a look and see if it helps
#chobo2, you can use quartz.net to check for due tasks (say, every one minute) and take some action (like sending an email) when a task has a due date less than the current date and the task was not notified.
So you will have to have a due date property in your task and a bit indicating if it has been notified.
Every minute, you run code that looks for tasks with due date less than or equal than the current date and for each of them you send a notification email. Then you mark the task as notified.
Regards.
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