Sending bulk emails from ASP.NET - asp.net

In the blog I'm working on currently whenever an user posts a comment for an article I like to send emails to all the other users commented for that post. What is the best way in sending mails in this case either synchronously or asynchronously? Can anyone share a code snippet for sending email to a list of users using System.Net.Mail in .NET.

You might want to use a separate webservice for sending emails. You app will send the body and email list to the webservice. You can use gzip or zip compression. This would make the webservice call very efficient(70%+ compression as the data is text).
Now in the webservice you can use multi-threading or Async operations for sending emails.
Edit: If you have a dedicated server, making a windows service might be a better option. Make a two tables in sql server, Queued Emails and Send Emails(Archive). So whenever user posts a comment, update the Queued emails table. Now the windows service can wake up every 30 seconds, extract all the rows from Queued emails and async send all of them. After an email is send, move/remove the row from the table.

Have a look at this
Sending Email with System.Net.Mail
MailMessage message = new MailMessage();
message.From = new MailAddress("sender#foo.bar.com");
message.To.Add(new MailAddress("recipient1#foo.bar.com"));
message.To.Add(new MailAddress("recipient2#foo.bar.com"));
message.To.Add(new MailAddress("recipient3#foo.bar.com"));
message.CC.Add(new MailAddress("carboncopy#foo.bar.com"));
message.Subject = "This is my subject";
message.Body = "This is the content";
SmtpClient client = new SmtpClient();
client.Send(message);

I use this to send mails asynchronously
SmtpClient smtp = new SmtpClient();
foreach(var mail in mailsToSend)
{
new Thread(() =>
{
smtp.Send(mail);
}
).Start();
}

If you are considering a third-party service, there is a decent article on getting started with Amazon's SES Email using .NET.
This worked very well for me, but note that SendGrid just announced new pricing equal to the Amazon's SES, SendGrid includes an SMTP wrapper so the code is even simpler.

I posted another StackOverflow answer addressing this issue of sending lots emails which can find by clicking here.
It is of course better to send email asynchronously to be able to maximize on how many can be sent in a fixed time frame. You can do this yourself or use already written components to do it.
I use Fluent.NET Mail to construct and send single emails and I use MassMailer.NET to send large volumes of emails.
Fluent.NET Mail
new Mail()
.Html(new MessageTemplate()
.FromText("This is my email with {content} text")
.DataFrom(new { content = "html" })
.BetweenBraces())
.To(new MailBox(emailAddress))
.From(new MailBox(emailAddress))
.Subject("Fluent API .NET 2")
.Timeout(5000)
.SmtpServer("[your smtp server]")
.SendAsync();
Check out the post.
MassMailer.NET
Check out this post for an example on how to send a large number of emails.

MailMessage message = new MailMessage();
message.From = new MailAddress("sender#foo.bar.com");
message.To.Add(new MailAddress("recipient1#foo.bar.com"));
message.To.Add(new MailAddress("recipient2#foo.bar.com"));
message.CC.Add(new MailAddress("carboncopy#foo.bar.com"));
message.Subject = "This is my subject";
message.Body = "This is the content";
SmtpClient client = new SmtpClient();
client.Send(message);
You can find more info here or here if you need to use NetworkCredential for authentication.
Sending it synchronously? Well, it depends how many you have to send. It can be time consuming.
In the situation where I have to send bulk emails and I do not want to hung my web server I develop a custom windows services which scans the DB periodically, collects info and sends emails.

Related

The server response was: 5.7.57 SMTP - Office 365

Currently we have a web form that send's emails which has stopped working months back with the migration of Office 365 at our company. I have eliminated all other code while troubleshooting and just run email portion to get the following error : "System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM ".
I have found many solutions on the web/stack overflow with the same error code, tried changing the code to proposed solutions and have had no luck.
Tried many different email aliases that are listed in our global address list with no luck for my account, different ports(25,587), different smtp addresses, and have had no luck so far.
Multiple users are stating "This code unfortunately is no longer valid with Office 365. Error message that comes up is The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [HE1PR05CA0133.eurprd05.prod.outlook.com] " on the article linked below.
Send SMTP email using System.Net.Mail via Exchange Online (Office 365)
My code seen below.
Dim mail As MailMessage = New MailMessage
mail.From = New MailAddress("email12345#company.com")
mail.To.Add("email12345#company.com")
mail.Subject = "Test"
mail.IsBodyHtml = False
mail.Body = "Test"
Dim SmtpServer As SmtpClient = New SmtpClient
SmtpServer.Host = "smtp.office365.com"
SmtpServer.Port = 587
SmtpServer.UseDefaultCredentials = False
SmtpServer.Credentials = New System.Net.NetworkCredential("MyEmail", "MyPass")
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network
SmtpServer.EnableSsl = True
SmtpServer.TargetName = "STARTTLS/smtp.office365.com"
Try
SmtpServer.Send(mail)
Catch ex As Exception
Response.Write(ex.ToString)
End Try
As it seems this is happening to many people, I would like to know what they are doing to resolve it?
I resolved this issue by creating app password for my outlook365 account. You can create/manage app password as following.
Go to My Account >> Security & Privacy >> Additional Security Verification >> Create and manage app passwords
Create an app password and use it in your code. Hope this will solve your issue
To enable app-password you must have two factor authentication on the account and the administrator (if your account is in an organization) must allow app-passwords on the account (or in the organization)
I'm not a 365 admin and don't know all the settings.
However, we had a similar problem where the sender account did not have the setting "Modern Authentication" enabled for SMTP AUTH.
note that this worked for a long time and suddenly stopped (not sure if Microsoft did some security tightening in late February 2021)
Modern Authentication was enabled for Outlook etc (clients?),
but not for "SMTP AUTH", "IMAP", and "POP"
(I assume this is the "defaults" and recommendation is not to enable Modern Authentication unless you need it.
see the two articles below.
https://learn.microsoft.com/en-us/exchange/clients-and-mobile-in-exchange-online/authenticated-client-smtp-submission
Virtually all modern email clients that connect to Exchange Online mailboxes in Office 365 or Microsoft 365 (for example, Outlook, Outlook on the web, iOS Mail, Outlook for iOS and Android, etc.) don't use SMTP AUTH to send email messages.
Therefore, we highly recommend that you disable SMTP AUTH in your Exchange Online organization, and enable it only for the accounts (that is, mailboxes) that still require it. There are two settings that can help you do this:
An organization-wide setting to disable (or enable) SMTP AUTH.
A per-mailbox setting that overrides the tenant-wide setting.
Also good to know about. Will list number of "smtp auth clients".
https://learn.microsoft.com/en-us/microsoft-365/security/office-365-security/mfi-smtp-auth-clients-report?view=o365-worldwide

Unable to send email from web application

I've looked this up and it seems other people have had the same issue mainly because Gmail was blocking the application's access, however I've turned on the "allow less secure apps to connect" option and the problem remains.
Here's the complete error message I get from ASP.net when I try to connect.
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
I've tried connecting using port 465 and I get a time-out (I assumed it was the Explicit/Implicit SSL problem)
Here's the code that I'm using to send the emails.
Dim Mail As New MailMessage
Dim EmailToMail As New MailAddress(EmailTo, EmailToName)
Mail.To.Add(EmailToMail)
Dim FromE As New MailAddress(EmailFrom, "Something")
Mail.From = FromE
Mail.Body = Body
Mail.Subject = Title
Dim SmtpClient As New SmtpClient("smtp.gmail.com", 587)
SmtpClient.UseDefaultCredentials = False
Dim SmtpCredentials As New System.Net.NetworkCredential("MyEmail", "MyPassword")
SmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network
SmtpClient.EnableSsl = True
SmtpClient.Send(Mail)
I've also tried setting UseDefaultCredentials to True, which didn't work.
your credentials are stored only within variable not within smtp instance.
set
SmtpClient.Credentials = SmtpCredentials
also make sure you use System.Net.Mail
This can happen if your password no longer meets Gmail's complexity criteria. Try logging in and changing it.
There can be a few issues.
Your firewall could be blocking it... (bypass it)
The port is blocked... (you need to allow it)
If you have two-step verification enabled on your email account...
Your credentials you are creating are never applied to the server credentials.
I have already answered this same question before here and will answer all of your problems. It also includes a link for sending the emails that I have answered as well.

Email goes to spam instead of inbox

We have a domain name "www.mycloudcctv.com" at godaddy.com and we have created a sub domain cam.mycloudcctv.com which points to 212.78.237.157
We have an application running on third party server (212.78.237.157). This application wants to send an email on our behalf using the email address “alerts# mycloudcctv.com ". Following code snippet (ASP.NET) is being used to send the email from (212.78.237.157)
var mailClient = new SmtpClient();
mailClient.Credentials = new NetworkCredential { UserName = "alerts#mycloudcctv.com", Password = "xyz" };
mailClient.Port = 25;
mailClient.Host = "smtpout.secureserver.net";
mailClient.EnableSsl = false;
var mail = new MailMessage("alerts#mycloudcctv.com", "azharmalik3#hotmail.com", "Test Smtp server", "Testing mycloudcctv server") { IsBodyHtml = true };
//Now Send the message
mailClient.Send(mail);
Everything works fine and emails are being sent however they end up in SPAM/JUNK folders of gmail/hotmail/yahoo. Could you please provide us necessary information so that our emails go to inbox instead of spam folders?
this is a BIG question with lots of complex issues, but it really boils down to three main areas:
Does the email come from a server which has be delegated the authority to deliver emails for the specified domain?
Is the content of the email just hyperlinks and does it contain text which would trigger spam assassin to mark as spam.
Is your server blacklisted for spam
For point 1 look into how to setup SPF records for send authority. http://www.mydigitallife.info/how-to-set-up-and-create-sender-policy-framework-spf-domain-dns-txt-record-with-wizard/
For point 2 get a copy of spam assassin and run your emails through it to see the score.
http://spamassassin.apache.org/
For point 3 http://whatismyipaddress.com/blacklist-check
This is not that easy. There are fair few things you have to do. For example SendGrid has some guidelines:
http://support.sendgrid.com/entries/21194967-deliverability-101
I found this blog-posting extremely useful! Give it a good read, it covers a lot of the points already mentioned here thus far:
http://www.codinghorror.com/blog/2010/04/so-youd-like-to-send-some-email-through-code.html

How to check MailMessage was delivered in .NET?

Is there a way to check if SmtpClient successfully delivered an email? SmtpClient.Send() does not appear to return anything other than an exception. So if the method completes is it safe to assume the email will be successfully sent to the appropriate email box? My code is below:
MailMessage emailMessage = new MailMessage();
emailMessage.Subject = SubjectText;
emailMessage.IsBodyHtml = IsBodyHtml;
emailMessage.Body = BodyText;
emailMessage.From = new MailAddress(Settings.Default.FromEmail,Settings.Default.FromDisplayName);
//add recipients
foreach (string recipientAddress in RecipientAddresses.Split(new char[{','},StringSplitOptions.RemoveEmptyEntries))
emailMessage.To.Add(recipientAddress);
using (SmtpClient smtpClient = new SmtpClient())
{
smtpClient.Send(emailMessage);
}
No, there is no reliable way to find out if a message was indeed delivered.
Doing so will require access to the end SMTP server for every person you are emailing.
If you do not get an exception, you can assume that the SMTP server did its best to deliver the email.
There's no way to be 100% sure that a mail message has been received when sent via SmtpClient due to the way email works. The fact that SmtpClient doesn't throw an exception essentially means that you've done everything right, but a failure can happen further down the line, for example:
The receiving mail server could reject the mail
An intermediate mail server could reject the mail
The server that SmtpClient is transmitting mail through could decide to refuse to transmit the mail
One solution you could use is to create an httphandler for your website images. If you send an HTML message which includes at least 1 image, then you could embed querystring data to the end of that image. This could even be something like a 1x1 transparent image. When the user reads the email, this sends the request to the server to fetch the image data, and in turn, you could capture that request and denote that the message was read.
This is not bulletproof however, because most email clients block images by default unless the user specifies they would like to view images in the email.
If the recipient e-mail address is valid you don't get an immediate return value about the successful delivery of the message; see the signature:
public void Send(MailMessage message)
The SMTP server will notify the sender (or whoever you specify for the notification) almost immediately with an 'Undeliverable' notification whenever the recipient e-mail address is invalid/fake.
SMTP servers are required to periodically retry delivery. When the recipient e-mail address is a valid address but for some reason the SMTP server could not deliver the message, the SMTP server will return a failure message to the sender if it cannot deliver the message after a certain period of time.
RFC 2821 contains more details.
From section 2.1 Basic Structure
In other words, message transfer can occur in a single connection
between the original SMTP-sender and the final SMTP-recipient, or can
occur in a series of hops through intermediary systems. In either
case, a formal handoff of responsibility for the message occurs: the
protocol requires that a server accept responsibility for either
delivering a message or properly reporting the failure to do so.
See sections 4.5.4 and 4.5.5
From section 6.1 Reliable Delivery and Replies by Email
If there is a delivery failure after acceptance of a message, the
receiver-SMTP MUST formulate and mail a notification message. This
notification MUST be sent using a null ("<>") reverse path in the
envelope. The recipient of this notification MUST be the address from
the envelope return path (or the Return-Path: line).

How can I send local Email by using exchange with asp.net?

I have a project that need to sends notification for employees by local email. I use SmtpClient class to send email but didn't work! here is the code:
MailMessage message = new MailMessage();
message.From = new MailAddress("localmail1#company.com");
message.To.Add(new MailAddress("localmail2#company.com"));
message.Subject = "Sending mail";
message.Body = "Check sending email by Exchange from asp.net code <> ";
SmtpClient client = new SmtpClient("ExchangeDNS", 25);
try
{
client.Send(message);
}
catch (Exception exc)
{
Label1.Text = exc.Message.ToString();
}
When I click buttons it give me an SmtpException with message : Failure sending mail.
NB: we use Exchange server.
How can I solve it?
Use the fully qualified name for your server, for example, exchangeDNS.example.com. If that doesn't work, try the IP address. You may want to manually telnet to port 25 on the Exchange server, just to see if it is possible. Also, check whether your server requires an authenticated or encrypted connection. If so, you'll need to supply credentials with the request or change to use SSL and the secure port.
Assuming that you have the server and any required credentials correct (double check these) I would try increasing the timeout on the SMTP client.
Your code is fine. It's the configuration of your Exchange server that is suspect. Some options for your Exchange administrator:
Allow anonymous SMTP connections.
Whitelist the IPs, so that only your web servers can send mail in this manner.
Create an Active Directory account for the web server, and then have your class authenticate using a username and password.
If you post more details about your Exchange configuration, I can help, at the risk of having this question downmodded for "not programming related".

Resources