System.Net.Mail and authentication headers - c#

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.

Related

how do i use MailMessage with MFA or send emails with MFA enabled?

I am making an app with ASP.Net and I am using Sytem.Net.Mail.MailMessage function to send emails when a new entry is added to the database, but my business is using MFA on its accounts. I was wondering, whether there is any way to circumvent MFA; without the need to disable it in the first place.
Example code
MailAddress to = new MailAddress("email2#domain.com");
MailAddress from = new MailAddress("email1#domain.com");
MailMessage message = new MailMessage(from, to);
message.Subject = "New item added";
message.Body = "A new item has been added to the databse and is waiting approval.";
SmtpClient client = new SmtpClient("outlook.office365.com", 587)
{
Credentials = new NetworkCredential("email1#domain.com", "Password"),
EnableSsl = true
};
// code in brackets above needed if authentication required
try
{
client.Send(message);
}
catch (SmtpException ex)
{
Console.WriteLine(ex.ToString());
}

Send email through smtp in c# through more than one from email addresses and to more than one receiver

I have project related school management system in c# using asp.net as framework and sql server as database.
I need to send email with same email body and subject (like: wish you a very happy new year) but i have different from addresses and obviously different to addresses.
For example: i need to send email to all teachers with aa#aa.aa email and to all students with bb#bb.bb email address and to management staff with cc#cc.cc
How can i perform this task in c# and asp.net with efficient way?
You can create SmtpClient and MailMessage object in order to send email.
I think you should call your mail sending method with 3 times with different parameters (from address and receivers list) Your methos looks like
SendMail("aa#aa.aa", teacherList);
SendMail("bb#bb.bb", studentList);
SendMail("cc#cc.cc", staffList);
public static void SendMail(string fromAddress, List<string> emailAddresses)
{
//make this smtpClient global. Because you will use next time.
SmtpClient smtpClient = new SmtpClient();
smtpClient.Credentials = new System.Net.NetworkCredential("smtpUserName", "smtpPassword");
smtpClient.Host = "mail.mailhost.com";//set your smtp host. Generally mail.domain.com
smtpClient.Port = 587;//set your smtp port
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = false;//you can change this based on your settings
MailMessage mail = new MailMessage();
mail.From = new MailAddress(fromAddress, "Our School Name");
for (int i = 0; i < emaillAddresses.Count; i++)
{
mail.Bcc.Add(new MailAddress(emaillAddresses[i]));
}
mail.Subject = "Wish you a very happy new year";
mail.IsBodyHtml = true;
mail.BodyEncoding = System.Text.Encoding.UTF8;
smtpClient.Send(mail);
}

How to save and open SMTP mail in ASP.Net

In my ASP.Net i have to save and open the SMTP mail.I am using SMTP for sending mail.At the same time i have to save it on "Folder" in the application and i want to open it how can i do that?
You need to instruct the SMTP client to save to a specific directory. The method below will save the MailMessage object as a .eml file.
MailMessage msg = new MailMessage();
msg.To.Add("[ToEmail]");
msg.From = new MailAddress("[FromEmail");
msg.Subject = "[YourSubject]";
SmtpClient client = new SmtpClient("[YourSmtpHost]");
client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
client.PickupDirectoryLocation = #"C:\[YourDirectory]";
client.Send(msg);

gmail Conversation via smtp

how can i send an email as a part of a gmail-conversation via smtp?
Taking the same subject doesnt work...
tell me if you need more infos...
thanks in advance!
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("#googlemail.com");
mail.To.Add("#.com");
mail.Subject = "(Somee.com notification) New order confirmation";
mail.Body = "(Somee.com notification) New order confirmation";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("", "");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
You'll need to use the following:
mail.Headers.Add("In-Reply-To", <messageid>);
The message id you should be able to get from the previous email's headers. Just look for "Message-Id".
This answer gives a few more headers you may want to add to try help threading in other clients. It seems maybe gmail is now using these, too.

asp.net email SmtpMail.SmtpServer.Insert(0, "");

I have a asp.net page that send an email to me.
SmtpMail.SmtpServer.Insert(0, ""); work fine.
What does it mean ? when I change it to SmtpMail.SmtpServer = "127.0.0.1";, it fails.
When I say SmtpMail.SmtpServer.Insert(0, ""), what I am exactly setting as my SMTP server ?
Actually
SmtpMail.SmtpServer.Insert(0, "");
does nothing.
SmtpServer property is of type String so you are basically calling string.Insert(int, string) which does not affect the string that you are calling insert on but returns a new instance of string with the with what you are trying to insert.
SmtpMail.SmtpServer = "google.com";
SmtpMail.SmtpServer = SmtpMail.SmtpServer.Insert(0, "mail.");
// now SmtpMail.SmtpServer will be "mail.google.com"
Just so you know, SmtpServer is obsolete. You should use SmtpClient instead: http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx
SmtpClient client = new SmtpClient();
//...
MailMessage message = new MailMessage(from, to);
// setup mail properties...
client.Send(message);

Categories