از این متد برای ارسال ایمیل در سی شارپ C# استفاده می شود. در این روش از SMTP استفاده شده است:
کد:
public void UDF_Authenticate(String Str_EmailTo)
{
//create the mail message
MailMessage mail = new MailMessage();
String Str_PassWord = GetUniqueKey(5);
//set the addresses
mail.From = new MailAddress("info@a00b.com");
mail.To.Add(Str_EmailTo.Trim());
//set the content
mail.Subject = "Password Information";
mail.Body = "<div style=\"direction: ltr;\"><span style=\"font-size: large;\"><u><em><strong> Your New PassWord:</u> " + Str_PassWord.Trim() + " </strong></em></span></div><div style=\"direction: ltr;\"><a href=\"http://www.a00b.com\"><span style=\"font-size: large;\"><u><em><strong>www.a00b.com</strong></em></u></span></a></div>";
mail.IsBodyHtml = true;
//send the message
SmtpClient smtp = new SmtpClient("mail.a00b.com");
smtp.Port = 25;
//to authenticate we set the username and password properites on the SmtpClient
smtp.Credentials = new NetworkCredential("info@a00b.com", "**********"); // در این محل اطلاعات کاربری ایمیل قرار می گیرد
smtp.Send(mail);
}
private String GetUniqueKey(int maxSize)
{
char[] chars = new char[62];
chars =
"ABCDEFGHJKLMNPQRSTUVWXYZ123456789".ToCharArray();
byte[] data = new byte[1];
RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
crypto.GetNonZeroBytes(data);
data = new byte[maxSize];
crypto.GetNonZeroBytes(data);
StringBuilder result = new StringBuilder(maxSize);
foreach (byte b in data)
{
result.Append(chars[b % (chars.Length)]);
}
return result.ToString();
}
و این هم یک متد دیگه که کمی کاملتره:
کد:
try
{
SmtpClient mySmtpClient = new SmtpClient("my.smtp.exampleserver.net");
// set smtp-client with basicAuthentication
mySmtpClient.UseDefaultCredentials = false;
System.Net.NetworkCredential basicAuthenticationInfo = new
System.Net.NetworkCredential("username", "password");
mySmtpClient.Credentials = basicAuthenticationInfo;
// add from,to mailaddresses
MailAddress from = new MailAddress("test@example.com", "TestFromName");
MailAddress to = new MailAddress("test2@example.com", "TestToName");
MailMessage myMail = new System.Net.Mail.MailMessage(from, to);
// add ReplyTo
MailAddress replyto = new MailAddress("reply@example.com");
myMail.ReplyTo = replyto;
// set subject and encoding
myMail.Subject = "Test message";
myMail.SubjectEncoding = System.Text.Encoding.UTF8;
// set body-message and encoding
myMail.Body = "<b>Test Mail</b><br>using <b>HTML</b>.";
myMail.BodyEncoding = System.Text.Encoding.UTF8;
// text or html
myMail.IsBodyHtml = true;
mySmtpClient.Send(myMail);
}
catch (SmtpException ex)
{
throw new ApplicationException
("SmtpException has occured: " + ex.Message);
}
catch (Exception ex)
{
throw ex;
}