csharp C#中的电子邮件发送
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp C#中的电子邮件发送相关的知识,希望对你有一定的参考价值。
protected void sendHtmlEmail(string from_Email, string to_Email, string cc_Email, string body, string from_Name, string Subject)
{
//create an instance of new mail message
MailMessage mail = new MailMessage();
//set the HTML format to true
mail.IsBodyHtml = true;
//create Alrternative HTML view
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");
//Add view to the Email Message
mail.AlternateViews.Add(htmlView);
//set the "from email" address and specify a friendly 'from' name
mail.From = new MailAddress(from_Email, from_Name);
//set the "to" email address
mail.To.Add(to_Email);
//set the "to" email address
if (!string.IsNullOrEmpty(cc_Email.Trim()))
mail.CC.Add(to_Email);
//set the Email subject
mail.Subject = Subject;
//set the SMTP info
string SMTP_HostName = "smtp3.hp.com";
int SMTP_Server_Port = 25;
SmtpClient smtp = new SmtpClient(SMTP_HostName, SMTP_Server_Port);
//send the email
smtp.Send(mail);
}
//Type 2
public static string SendMail(String Sender,String Recipient,String CC,String Subject,String Content)
{
StreamReader sr = null;
try
{
string mailServer = ConfigurationManager.AppSettings["smptAddress"].ToString();
System.Net.Mail.MailMessage objMail = new System.Net.Mail.MailMessage();
System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient();
if (RequriedMail == true)
{
if (mailServer.Contains("gmail"))
{
#region Local Chimera Code
System.Net.Mail.MailMessage result = new System.Net.Mail.MailMessage(Sender, Recipient);
objMail.From = new MailAddress(Sender);
objMail.To.Add(Recipient);
objMail.IsBodyHtml = true;
objMail.Subject = Subject;
objMail.Priority = System.Net.Mail.MailPriority.Normal;
if (CC != "")
{
objMail.CC.Add(CC);
}
mailClient.UseDefaultCredentials = false;
System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["smptUid"].ToString(), ConfigurationManager.AppSettings["smptPwd"].ToString());
mailClient.Credentials = basicAuthenticationInfo;
mailClient.Host = mailServer;
mailClient.Port = Convert.ToInt32(ConfigurationManager.AppSettings["smptPort"]);
mailClient.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["smptSSl"]);
objMail.Body = Content;
mailClient.Send(objMail);
return true.ToString();
#endregion
}
else
{
#region HP Mail Code
objMail.From = new MailAddress(ConfigurationManager.AppSettings["smptUid"].ToString(), ConfigurationManager.AppSettings["smtpDisplayName"].ToString());
MailAddress toAddress = new MailAddress(Recipient);
objMail.To.Add(toAddress);
mailClient.UseDefaultCredentials = false;
mailClient.Host = mailServer;
mailClient.Port = Convert.ToInt32(ConfigurationManager.AppSettings["smptPort"]);
mailClient.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["smptSSl"]);
objMail.Priority = System.Net.Mail.MailPriority.Normal;
objMail.Subject = Subject;
objMail.Body = Content;
objMail.IsBodyHtml = true;
if (CC != "")
{
objMail.CC.Add(CC);
}
mailClient.Send(objMail);
return true.ToString();
#endregion
}
}
else
{
return true.ToString();
}
}
catch (Exception ex)
{
log.Error(ex.InnerException.ToString());
return false.ToString();
}
finally
{
if (sr != null)
{
sr.Close();
}
}
}
<add key="smptAddress" value="smtp.ggogle.com" />
<add key="smptUid" value="tcinotification@goo.com" />
<add key="smtpDisplayName" value="TCI Notification" />
<add key="smptPwd" value="chimera123" />
<add key="smptPort" value="25" />
<add key="smptSSl" value="true" />
<add key="mailsending" value="true"/>
</appSettings>
以上是关于csharp C#中的电子邮件发送的主要内容,如果未能解决你的问题,请参考以下文章