MailMessage with string instead of MailAddress in To - c#

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!

Related

System.Net.Mail and authentication headers

Using System.Web.Mail in an old project I used to use the following code to build an authenticated message
MailMessage msg = new MailMessage();
// ... fill in to, from etc
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", Application["smtpserver"].ToString());
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", Application["smtpserverport"].ToString());
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", 2);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", Application["sendusername"].ToString());
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", Application["sendpassword"].ToString());
System.Net.Mail is recommended as the replacement, but it seems to have done away with these fields.
Here's my new email sending code.
MailMessage em = new MailMessage();
// ... fill in to, from etc
// Init SmtpClient and send
SmtpClient smtpClient =
new SmtpClient(
AppSetting["smtpserver"],
Convert.ToInt32(AppSetting["smtpserverport"])
);
System.Net.NetworkCredential credentials =
new System.Net.NetworkCredential(
AppSetting["sendusername"],
AppSetting["sendpassword"]
);
smtpClient.Credentials = credentials;
smtpClient.Send(em);
I suspect that SmtpClient.Send is now doing all of this behind the scenes?
Yes, you don't have to add those fields manually.

How to find out Out Of Office flag from a mail header using C#

How do I find out "Out Of Office" flag from a received email using C#.
I dont have any scope to parse the Subject / Body. I need to find only thru either the header property if available. Or any other means.
Please suggest.
Thanks,
Sriram
You can add the custom header to the mailMessage as below,
MailMessage mail = new MailMessage();
mail.To = "me#mycompany.com";
mail.From = "you#yourcompany.com";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body.";
mail.Headers.Add( "X-Organization", "My Company LLC" );//Your custom header goes here
SmtpMail.SmtpServer = "localhost"; //your real server goes here
SmtpMail.Send( mail );
and letter you can access it as below,
IEnumerable<string> headerValues = mail.Headers.GetValues("X-Organization");
var id = headerValues.FirstOrDefault();
If you are using Outlook Object Model, check if the MailItem.MessageClass property is "IPM.Note.Rules.OofTemplate.Microsoft". This will only work if the sender is in the same domain as the receiver. Otherwise all bets are off - this is nothing special about the OOF messages.

ASP.NET emailing to multiple emails

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
}

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);

I want to send a textBox value in email in C#. some one Help me?

I want to send a textBox value in email in C#. some one Help me?
see Scott Gu's article to send Sending Email with System.Net.Mail here.
Load the body of the mail with the TextBox.Text value
txtBox.Text;
will give you the value in C#
TextBox.Text Property
you can try something like this
MailMessage msg = new MailMessage();
msg.From = "test#mail.com";
msg.To.Add("rec#mail.com");
msg.Subject = "test";
msg.Body = yourTextbox.Text
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = _smtpHostName; //host name
smtpClient.Port = _smtpPort; //required port
smtpClient.Send(msg);
Have a look at
SmtpClient Class
and SmtpClient Constructor (String, Int32)

Categories