ASP.NET emailing to multiple emails - c#

I have this code here...
MailAddress from = new MailAddress("noreply#fakeemail.com", "IPC Orders");
MailAddress to = new MailAddress("email1#fakeemail.com.com");
MailMessage mail = new MailMessage(from, to);
mail.To.Add("email2#fakeemail.com");
mail.To.Add("email3#fakeemail.com");
Obviously this is not the full code, but when I try to send an email to multiple email address is doesnt send, if I comment out these two lines...
mail.To.Add("email2#fakeemail.com");
mail.To.Add("email3#fakeemail.com");
It works and will send it to the first email MailAddress to = new MailAddress("email1#fakeemail.com.com");
Whats wrong with my code

USE AddressCollection FOR ADDING MULTIPLE TO ADDRESSES
LIKE
mail.To = new AddressCollection( "email2#fakeemail.com, email3#fakeemail.com");

you can try to add add all your email addresses to a list, then just iterate over that list and send a mail at each element
List<string> emailAddress = new List<string>();
emailAddress.add("email1#em.com");
emailAddress.add("email2#em.com"); // ... etc
foreach (string email in emailAddress)
{
MailMessage mail = new MailMessage(from, email);
//+ more stuff
}

Related

MailMessage with string instead of MailAddress in To

Is it possible to address a mailmessage to a 'name' instead of an 'e-mailaddress'.
Something like this:
MailMessage mmsg = new MailMessage();
mmsg.To.Add("Winfrey");
instead of:
MailMessage mmsg = new MailMessage();
mmsg.To.Add(new MailAddress("Winfrey#user.com"));
Is it possible to add something in the 'To' part without the '#' and domain? And if so, how?
The MailMessage class does not provide this. From where should this class know, which mail address is behind 'Winfrey'? The only way is to create a simple address book.
Then you can write this
mmsg.To.Add(_addressBook["Winfrey"]);
_addressBook is a Dictionary here.
Dictionary<string, string> _addressBook = new Dictionary<string,string>();
Does this help?
If you are looking to see the sender's name on the e-mail that the other user receives:
MailMessage mail = new MailMessage();
mail.From = new MailAddress("winfrey#user.com", "winfrey" );
It'll display the name Winfrey.
System.Net.Mail doesn't validate the email that you are sending to. MailMessage.To only accepts email address. See this!

SendGrid "To" List email ids visible to everyone in the list

I am using SendGrid to send emails to a list of users through a console application in asp.net. I am sending a list of user's email addresses in the AddTo section while sending an email. The code looks like this:
SendGridMessage message = new SendGridMessage();
message.AddTo(new List<string>() { "user1#abc.com", "user2#xyz.com", "user3#abc.com", "user4#xyz.com" });
The email is sent as expected, but in the "To" section of the email, I am able to see all the email ids of users to whom this email was sent(image attached below). I want the email ids to be hidden so that no one misuses the other email ids in the list. Is there anyway I can accomplish this using SendGrid?
Use .AddBcc() instead of .AddTo(). BUT if you do this then you'll have to set the To address to something like "no-reply#example.com" which isn't ideal and might increase the chances of the message ending up in the SPAM or Junk folders of your users.
SO instead, write a for loop to send the email per user.
var emailAddresses = new List<string>() { "user1#abc.com", "user2#xyz.com", "user3#abc.com", "user4#xyz.com" };
for (var emailAddress in emailAddresses)
{
var email = new SendGridMessage();
email.AddTo(emailAddress);
// set other values such as the email contact
// send/deliver email
}
Is the content of the email message the same for everyone? I would assume each person would have different "monthly usage" amounts and if so the for loop would be better...
To send to multiple recipients in SendGrid without them seeing each other, you want to use the X-SMTPAPI header, as opposed to the native SMTP To header.
var header = new Header();
var recipients = new List<String> {"a#example.com", "b#exampe.com", "c#example.com"};
header.SetTo(recipients);
var subs = new List<String> {"A","B","C"};
header.AddSubstitution("%name%", subs);
var mail = new MailMessage
{
From = new MailAddress("please-reply#example.com"),
Subject = "Welcome",
Body = "Hi there %name%"
};
// add the custom header that we built above
mail.Headers.Add("X-SMTPAPI", header.JsonString());
The SMTPAPI header will be parsed by SendGrid, and each recipient will get sent a distinct single-To message.
You need to use personalization for that, there are 2 options:
var message = new SendGridMessage();
message.Personalizations = new List<Personalization>();
foreach(var email in emails)
{
var personalization = new Personalization();
personalization.AddTo(new Email(email));
}
Or:
var message = new SendGridMessage();
foreach(var email in emails)
message.AddTo(email, emails.IndexOf(email));
Alternatively you can also use MailHelper with the showAllRecipients flag:
MailHelper.CreateSingleEmailToMultipleRecipients()

How to send group emails asp.net/C# depending on select query

I am trying to send a group email depending on emails gathered from a select query called emailresults.
This is the code I have got so far but I am receiving error:
Could not send the e-mail - error: The specified string is not in the form required for an e-mail address
MailMessage message = new MailMessage("queensqsis#gmail.com", "queensqsis#gmail.com");// to & from
message.To.Add(emailresult);
message.Subject = "Test";
message.Body = "test ";
SmtpClient Client = new SmtpClient();
Client.Send(message);
If emailResults is some kind of Enumerable you will need to add each email address string from the Enumerable to the MailMessage.To MailAddressCollection. It's hard to say exactly how to do this without knowing the Type of emailresults. I expect you need something along these lines.
MailMessage message = new MailMessage("queensqsis#gmail.com", "queensqsis#gmail.com");// to & from
for(var item in emailresult){
message.To.Add(item);
}
message.Subject = "Test";
message.Body = "test ";
SmtpClient Client = new SmtpClient();
Client.Send(message);
If you can confirm what Type emailResults is it will be much easier to provide a clear solution.
In your comment above you state that emailresult is a select query result, so some sort of enumarable? Or as another poster suggested a long string separated by commas or semicolons?
Try doing one of these suggestions...
MailMessage message = new MailMessage("queensqsis#gmail.com", "queensqsis#gmail.com");// to & from
//For each item in the list, add it to the list of "To" recipients
emailresult.ForEach(r => message.To.Add(r)) ;
//OR IF THE ADDRESS IS A PROPERTY OF THE ITEM
emailresult.ForEach(r => message.To.Add(r.MyEmailAddressProperty)) ;
//OR IF emailresult is some sort of string list, "email1,email2,email3" or similar then
foreach (string oneEmail in emailresult.Split(","))
{
message.To.Add(oneEmail);
}
message.Subject = "Test";
message.Body = "test ";
SmtpClient Client = new SmtpClient();
Client.Send(message);

Mail doesn't work with multiple to addresses [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Unable to send an email to multiple addresses/recipients using C#
I have used below code to send mail in script task
string MailFromName = "Admin";
System.Net.Mail.SmtpClient mailobj = new System.Net.Mail.SmtpClient();
System.Net.Mail.MailAddress MailFrom = new System.Net.Mail.MailAddress(MailFromEmail, MailFromName);
System.Net.Mail.MailAddress MailTo = new System.Net.Mail.MailAddress(MailToEmail, MailToEmail);
System.Net.Mail.MailMessage mailmsg = new System.Net.Mail.MailMessage(MailFrom, MailTo);
mailmsg.IsBodyHtml = true;
mailmsg.Subject = strMessageSubject;
mailmsg.Body = strMessageBody;
mailobj.Host = strSMTPServerName;
mailobj.Send(mailmsg);
It is working fine when I am using MailToEmail as "myaddress#myMail.com" i.e. for one email address
but this doesn't send any mail(also it dosen't fail) when I have multiple adress in to list
ex: "MyAdress#MyMail.com; MySecondAddress#MyMail.com"
How to resolve this?
EDIT New Code
string MailFromName = "Admin";
System.Net.Mail.SmtpClient mailobj = new System.Net.Mail.SmtpClient();
System.Net.Mail.MailAddress MailFrom = new System.Net.Mail.MailAddress(MailFromEmail, MailFromName);
System.Net.Mail.MailAddress MailTo = new System.Net.Mail.MailAddress(MailToEmail, MailToEmail);
System.Net.Mail.MailMessage mailmsg = new System.Net.Mail.MailMessage(MailFrom, MailTo);
mailmsg.IsBodyHtml = true;
mailmsg.Subject = strMessageSubject;
mailmsg.Body = strMessageBody;
foreach (string str in multipleToMsg)
{
mailmsg.To.Add(str);
}
mailobj.Host = strSMTPServerName;
mailobj.Send(mailmsg);
You've not shown how exactly you are adding the recipients. However to add multiple recipients you add to the "To" collection:
MailMessage message = new MailMessage();
message.To.Add("sillyjoe#stackoverflow.com");
"To" is a collection of MailAddresses. Make sure you are adding it to that collection and not attempting to concatenate email addresses all into one MailAddress object.
Accoring to MSDN: MailMessage Class the "To" property is a collection of MailAddresses
so you just need to do something like
mailmsg.To.Add(new System.Net.Mail.MailAddress(MailToEmail, MailToEmail));
mailmsg.To.Add(new System.Net.Mail.MailAddress(MailToEmail2, MailToEmail2))
or in a foreach loop
//get email addresses into a collection called emailAdds
foreach (var emailAdd in emailAdds)
{
mailmsg.To.Add(new System.Net.Mail.MailAddress(emailAdd, emailAdd ));
}
To specify multiple addresses you need to use the To property which is a MailAddressCollection
message.To.Add("one#example.com, one#example.com"));
message.To.Add("two#example.com, two#example.com"));

Sending to multiple Email addresses but displaying only one C#

I am using the SmtpClient in C# and I will be sending to potentially 100s of email addresses. I don't want to have to loop through each one and send them an individual email.
I know it is possible to only send the message once but I don't want the email from address to display the 100s of other email addresses like this:
Bob Hope; Brain Cant; Roger Rabbit;Etc Etc
Is it possible to send the message once and ensure that only the recipient's email address is displayed in the from part of the email?
Ever heard of BCC (Blind Carbon Copy) ? :)
If you can make sure that your SMTP Client can add the addresses as BCC, then your problem will be solved :)
There seems to be a Blind Carbon Copy item in the MailMessage class
http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.aspx
http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.bcc.aspx
Here is a sample i got from MSDN
public static void CreateBccTestMessage(string server)
{
MailAddress from = new MailAddress("ben#contoso.com", "Ben Miller");
MailAddress to = new MailAddress("jane#contoso.com", "Jane Clayton");
MailMessage message = new MailMessage(from, to);
message.Subject = "Using the SmtpClient class.";
message.Body = #"Using this feature, you can send an e-mail message from an application very easily.";
MailAddress bcc = new MailAddress("manager1#contoso.com");
//This is what you need
message.Bcc.Add(bcc);
SmtpClient client = new SmtpClient(server);
client.Credentials = CredentialCache.DefaultNetworkCredentials;
Console.WriteLine("Sending an e-mail message to {0} and {1}.",
to.DisplayName, message.Bcc.ToString());
try {
client.Send(message);
}
catch (Exception ex) {
Console.WriteLine("Exception caught in CreateBccTestMessage(): {0}",
ex.ToString() );
}
}
If you are using the MailMessage class, make use of the BCC (Blind Carbon Copy) property.
MailMessage message = new MailMessage();
MailAddress bcc = new MailAddress("manager1#contoso.com");
// Add your email address to BCC
message.Bcc.Add(bcc);

Categories