C# 发送邮件

0000 2008-11-21 11:01:26



谁知道发件服务器和服务器端口怎么填吗,比如163
...全文
88 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ABC_678 2008-11-21
  • 打赏
  • 举报
回复
public static void MailSend(String Smtp, String FromAddr, String ToAddr, String Title, String Content, System.Text.Encoding Ecoding)
{
SmtpClient sClient = new SmtpClient(Smtp);
try
{
MailMessage message = new MailMessage(
FromAddr,
ToAddr,
Title,
Content);
message.BodyEncoding = Ecoding;
message.SubjectEncoding = Ecoding;
SmtpClient client = new SmtpClient(Smtp,80);//端口
client.Credentials = new System.Net.NetworkCredential("email address", "password");
client.Host = Smtp;
client.Send(message);//简单实现,异步实现方法 client.SendAsync(message, UserState);
client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);//事件

}
catch (Exception ex) { throw ex; }
finally { sClient = null; }
}
acqy 2008-11-21
  • 打赏
  • 举报
回复

public void SendMail(string fromAddr, string toAddr, string bcc, string cc, string subject, string body)
{
if ((fromAddr == null) || (fromAddr == ""))
{
fromAddr = "sender@contoso.com";
}

if ((toAddr == null) || (toAddr == ""))
{
throw new Exception("To address should not be empty.");
}

//replace ';' with ','
string toAddress = toAddr.Replace(";", ",");

// Instantiate a new instance of MailMessage
MailMessage mMailMessage = new MailMessage(fromAddr, toAddress);

// Check if the bcc value is null or an empty string
if ((bcc != null) && (bcc != string.Empty))
{
// Set the Bcc address of the mail message
mMailMessage.Bcc.Add(new MailAddress(bcc));
}

// Check if the cc value is null or an empty value
if ((cc != null) && (cc != string.Empty))
{
// Set the CC address of the mail message
mMailMessage.CC.Add(new MailAddress(cc));
}
// Set the subject of the mail message
mMailMessage.Subject = subject;
// Set the body of the mail message
mMailMessage.Body = body;
// Set the format of the mail message body as HTML
mMailMessage.IsBodyHtml = true;
// Set the priority of the mail message to normal
mMailMessage.Priority = MailPriority.Normal;

// Instantiate a new instance of SmtpClient
SmtpClient mSmtpClient = new SmtpClient(this.Host, this.Port);

mSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
//Credential
System.Net.NetworkCredential oCredential = new System.Net.NetworkCredential(Username, Password);
mSmtpClient.UseDefaultCredentials = false;
mSmtpClient.Credentials = oCredential;

// Send the mail message
mSmtpClient.Send(mMailMessage);
}
acqy 2008-11-21
  • 打赏
  • 举报
回复
发件服务器应该是smtp.163.com。端口好像是25
0000 2008-11-21
  • 打赏
  • 举报
回复
发件服务器我填过 smtp.163.com,pop.163.com,mail.163.com不知道哪个对呀,

111,131

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧