Hello i have a problem with sendig mails from asp.net web site. Here is my code
private void mailgonder(string mail, string adsoyad)
{
MailMessage mm = new MailMessage();
mm.From = new MailAddress("my mail address", "my name");
mm.Subject = "YENİ DUYURU";
mm.Body = "Sistemde okumadığınız yeni bir duyuru bulunmaktadır.";
mm.To.Add(new MailAddress(mail, adsoyad));
SmtpClient sc = new SmtpClient("my smtp client");
sc.Port = 587;
sc.Credentials = new NetworkCredential("my username", "my password");
sc.Send(mm);
}
I want to send mail to 1500 users with same time. How can i do this in asp.net (we will have over 1000 member in this web project)
I can send like this to one person. But i don't know how can i send this to multiple persons
Thanks
As MailMessage.To is a collection, you can add as much as you want :
mm.To.Add(new MailAddress("user#user.com", "user1"));
mm.To.Add(new MailAddress("user1#user.com", "user2"));
mm.To.Add(new MailAddress("user2#user.com", "user3"));
mm.To.Add(new MailAddress("user3#user.com", "user4"));
.....
until
mm.To.Add(new MailAddress("user1499#user.com", "user1499"));
mm.To.Add(new MailAddress("user1500#user.com", "user1500"));
NOTE :
Restrictions may exist according to your mail server or access provider. For more efficiency, use a mailing list.
You have to send each user seperate emails. Do not, never, add more than 1 address to TO unless you have sender score certification, doing opposite will cause you get banned from mail providers since they would think you are a spammer. And also spam list is global, which means if you get banned from gmail, you will also get banned from hotmail or yahoo in a short period of time.
Just select for example 20 email addresses from database each time and loop that 20 and call your mailgonder method for each.
Other than what I wrote above, if you write multiple TO's, each of your users will see each others email adressses which is not cool from user perspective.
I Believe the simplest approach by far is to make a contact group. Doing this will keep the amount of code written to a minimum. Here is a link explaining how to do so in Outlook How to make a contact group
Once you have the contact group setup, simply do this:
String Devemail = "devgroup#whatever.com";
MailMessage message = new MailMessage();
message.To.Add(Devemail);
//The rest of your code here
The rest of your code will work fine.
If someone has a better/simpler approach, please feel free to correct me.
EDIT: I am kind of shocked that we recommend he hard code 1500 email addresses into his code. Can someone give me a good reason for using that approach over using an email group?
Related
I am having an issue with sending emails specifically to Gmail-related accounts, and I'll be darned if I know what the issue is. This is a C# ASP.NET project, by the way.
First, the following code works, as long as I am sending to any email account OTHER than a Gmail account:
var mail = new MailMessage {
Subject = "test email",
Body = "this is only a test",
Priority = MailPriority.High,
IsBodyHtml = true,
From = new MailAddress ( "<outbound email here>" )
};
var msgID = Guid.NewGuid().ToString();
var sentBy="<outbound mail domain>";
mail.Headers.Add ( "message-id", $"<{msgID}>");
mail.Headers.Add ( "msg-id", $"<{msgID}#{sentBy}>");
mail.To.Add ( new MailAddress ( "<recipient email>" ) );
var smtpClient = new SmtpClient("<email server address>") {
Port = 587,
Credentials = new NetworkCredential("<sender's email address>", "<password>"),
};
smtpClient.Send ( mail );
I have removed email addresses and network credentials, obviously.
The code works, because as long as I send email to a NON-Gmail account, it comes through just fine. But anything going to a Gmail-related account never arrives.
I added the two lines in the code above to add a message ID to the header based on what I read in several older posts here about some mail servers, like Gmail, rejecting email messages that didn't include them, but it has not fixed the issue, and I'm out of ideas. My ISP says the SPF record for the mail server is fine, so according to them that's not the issue.
Has anyone else encountered this recently, and if so, how did you fix it?
To clarify, the comments/answers I have received so far are appreciated, but as I stated in the OP, this is a problem with sending emails TO Gmail accounts. I am using my ISP's mail server to send them, and I am adding a message ID to the header to address what the log says, that the message is missing a message ID and won't be accepted. I can send emails to other non Gmail accounts just fine, and when I inspect the headers they show a message id. So I don't know why this continues to be an issue.
So the answer to this is that the issue was related to changes Google made to policies for sending emails to Gmail accounts, which might require adjustments to the SPF record on the sending SMTP server. That was the case in this situation - the hosting company was slow to respond (took them more than a week) and had to elevate the issue to their Tier 3 techs, but once the SPF record was fixed to account for Google's changes, all was resolved.
We have an internal web application that sends email notifications when a form is submitted. It sends using an internal exchange server that is connected to Office 365.
The notifications go to a distribution group, and for years this has worked fine, but some weeks ago they started not getting some of the emails, roughly 50% of them. To try and troubleshoot this issue, I added myself to the CC on the test version of this internal site, and while the distro group still only receives roughly 50% of the notifications they should be receiving, I get 100% of them. If they submit 10 forms, they'll only receive 5 or so while I receive all 10.
Looking in the exchange trace logs, I can see most of the emails that send successfully to both the distro group and me, but not all of them. Also mysteriously, I cannot locate the emails that only send to myself at all.
To further complicate this, this all started when some users were errantly added to the distro group, and subsequently removed. Those users happened to be in a different domain than the others that were already in and still are in the group.
Here is the relevant code that sends the emails upon submission of the form, from a controller action in a .NET Core project.
var mailSettings = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build().GetSection("Mail");
var clientHost = mailSettings.GetValue<string>("clienthost");
var clientPort = mailSettings.GetValue<int>("clientport");
if (account.Status.Equals("Submitted"))
{
using (var mail = new MailMessage())
{
string body = "body here";
mail.Subject = "subject here";
#if DEBUG
mail.Subject = "TEST - " + mail.Subject;
#endif
mail.Body = body;
mail.From = new MailAddress(account.Email);
mail.To.Add("distrogroup#mycompany.fake");
#if DEBUG
mail.CC.Add("myemail#mycompany.fake");
#endif
mail.IsBodyHtml = true;
var smtp = new SmtpClient();
smtp.Host = clientHost;
smtp.Port = clientPort;
smtp.UseDefaultCredentials = false;
smtp.Send(mail);
}
}
The inconsistent behavior and missing log information is really making me scratch my head, so I'm looking for ideas on what I can do to continue troubleshooting. What steps would you take to narrow this issue down? Is there any additional information that I should provide?
Thank you in advance.
EDIT:
If I replace the distribution group with the individual email addresses that belong to it, they all get them 100% of the time. I'm really sure what would cause the distro group to fail some of the time, but I thought that was interesting.
I have developed a asp.net Mvc 4 project and now i am planing to integrate a Mail system in my application.Initially i taught like integrating mail System in my existing web application but later i moved it to a console application using Scheduler to send mail at some time interval.
My scenario is like i have a list of mail ids and i need to send mail to all these mail ids . I have checked System.Web.Mail and i found i can only give one email address at a time. Is it possible in System.Web.Mail or is there any other library available to achieve my scenario.
To in System.Net.Mail is a MailAddressCollection,so you can add how many addresses you need.
MailMessage msg = new MailMessage();
msg.To.Add(...);
msg.To.Add(...);
Chris's answer is correct but you may want to also consider using a mail service. Here are some you could try - they all have a free tier to get started on.
http://sendgrid.com/
http://www.mailgun.com/
https://mandrill.com/
http://aws.amazon.com/ses/
You can easily sent emails to more than one recipient. Here is a sample that uses a SMTP server to send an email to multiple addreses:
//using System.Net.Mail;
public void SendEmail(){
MailMessage email = new MailMessage();
email.To.Add("first#email.com");
email.To.Add("second#email.com");
email.To.Add("third#email.com");
email.From = new MailAddress("me#email.com");
string smtpHost = "your.SMTP.host";
int smtpPort = 25;
using(SmtpClient mailClient = new SmtpClient(smtpHost, smtpPort)){
mailClient.Send(email);
}
}
Just a note: if you go with SMTP, you should probably have a look also on MSDN for the SmtpClient.Send method, just to be sure you are catching any related exceptions.
So im implementing a mailservice in our webapp that users can utilize to send out emails to persons on a certain list. Now i need to know how to best use the System.Net.Mail object to send mail on the users behalf. Ive been trying this now for a while without the right results.
I would like the mail to read "our system on behalf of user 1" and the reply to adress should be the adress that user1 has in our system so that when the contacted person wants to reply to the mail he should get user1:s address, not ours. How can I do this?
Which fields do I need to declare and how? This is how i have it set up right now, but a reply to these settings sends a mail back to noreply#oursystem.com
from = 'noreply#oursystem.com'
replyTo = 'user1#privateaddress.com'
to = 'user1#privateaddress.com'
sender = 'user1#privateaddress.com'
cc = ''
ReplyTo is obsolete. You should use ReplyToList
Example:
MailAddress mailFrom = new MailAddress("noreply#oursystem.com");
MailAddress mailTo = new MailAddress("user1#privateaddress.com");
MailAddress mailReplyTo = new MailAddress("user1#privateaddress.com");
MailMessage message = new MailMessage();
message.From = mailFrom;
message.To.Add(mailTo); //here you could add multiple recepients
message.ReplyToList.Add(mailReplyTo); //here you could add multiple replyTo adresses
This was caused by a programming error on the receiving end. The correct solution is to set the from and replyto as above. That will get the correct behaviour.
i have few question about smtp
im using this code to send mails if the host is gmail then it act diffrente:
foreach (string host in hosts)
{
SmtpClient sc = null;
try
{
if (emailDomain.ToLower() == "gmail.com")
{
MailSend.MailSendApp.EventLog.WriteEntry("mail to gmail.com");
sc = new SmtpClient("smtp.gmail.com", 587);
sc.UseDefaultCredentials = false;
sc.DeliveryMethod = SmtpDeliveryMethod.Network;
sc.Credentials = new NetworkCredential("UID#gmail.com", "PWD");
sc.EnableSsl = true;
} }
else
{
sc = new SmtpClient(host);
sc.Send(mailMessage);
break;
}
is it possiable to get answer from smtp :
1. that the email arrived
2. if the mail exists
thanks
If you want to receive a notification that the email has arrived you need to send the email with delivery notification options.
mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
If the email doesn't exists, you will get an email back to your sender address and not to your SMTP class.
In short, there's no easy way to determine these two thing purely from the SMTP class perspective.
Back in the day you could directly query whether the email address exists on a given server (VRFY user SMTP command). Then, SPAM was born and pretty much all email servers removed that capability because spammers would use bots to query possible email recipients on each mail server to build SPAM lists.
You will still get an ordinary bounce report (email back to the reply-to address indicating that delivery failed). I use a tool called Boogie Tools to automate processing of bounce messages.
Gmail does not offer delivery or read receipts (though some email servers still optionally allow it) for similar reasons... too much abuse potential.