.NET 2.0 发送EMail邮件
2007-11-05 22:31
609 查看
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
protected void Button1_Click(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//收件人
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
message.To.Add(txtMailAddress.Text);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//抄送
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
foreach(string mailAddress in txtCopyAddress.Text.Split(','))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (!string.IsNullOrEmpty(mailAddress))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
message.CC.Add(mailAddress);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//主题
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
message.Subject = txtSubject.Text;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
message.From = new System.Net.Mail.MailAddress(fromAddress);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
message.Body = txtMailContents.Text;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//发送附件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (fulAttachment.HasFile)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(fulAttachment.PostedFile.FileName);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
message.Attachments.Add(attachment);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.Net.Mail.SmtpClient smtp =<
4000
/span> new System.Net.Mail.SmtpClient(txtSMTPAddress.Text);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
smtp.Credentials = new System.Net.NetworkCredential(txtUserName.Text,txtPassword.Text);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//smtp.usedefaultcredentials = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
try
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
smtp.Send(message);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lblReturnMessage.Text = "Mail Send Success!";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
catch (System.Net.Mail.SmtpException ex)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
lblReturnMessage.Text = "Send Error:" + ex.Message;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
如果选择了163的SMTP服务器,“smtp.163.com”,发件人必须是登录用户的163 email用户地址。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.163.com");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
smtp.Credentials = new System.Net.NetworkCredential("163emailaddress","emailpassword");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
message.From = new System.Net.Mail.MailAddress("163emailaddress@163.com");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
web.config加入
<system.net>
<mailSettings>
<smtp from="163emailaddress@163.com">
<network host="smtp.163.com" password="*****" port="25" userName="username" defaultCredentials="false"/>
</smtp>
</mailSettings>
</system.net>
以下为VB.NET代码
Dim message As New MailMessage()
'收件人
message.To.Add("one@21cn.com")
'主题
message.Subject = "使用.NET 2.0发送邮件"
message.From = New MailAddress("163emailaddress@163.com")
message.Body = "非常简单"
Dim m_smtpClient As New SmtpClient("smtp.163.com")
m_smtpClient.Send(message)
相关文章推荐
- Microsoft .NET Framework 2.0实现发送邮件(Email)总结
- 在.NET 2.0 中发送Email
- 使用 .NET 2.0 的 SmtpClient 类发送邮件
- Microsoft .NET Framework 2.0实现发送邮件(Email)总结
- 在.NET 2.0 中发送Email
- .Net 2.0 中的发送邮件实现小解
- 基于 .NET 2.0 System.Net.Mail namespace 的邮件发送组件
- .net 2.0 下发送邮件的方式。System.Web.Mail 过时用System.Net.Mail
- .NET 发送EMAIL邮件
- 在.NET 2.0 中发送Email
- .NET 2.0 下的邮件发送程序 Web.Mail
- .net 2.0 中发送Email
- Microsoft .NET Framework 2.0实现发送邮件(Email)总结
- Microsoft .NET Framework 2.0实现发送邮件(Email)总结
- .net SMTP发送Email邮件且可带附件示例
- 在.NET中发送邮件,使用回执功能中遇到问题的解决方法[在NOTES服务器上,C#,.NET2.0环境]
- .Net 2.0 中的发送邮件实现小解(轉)
- [转]在.NET 2.0 中发送Email
- .net SMTP发送Email邮件且可带附件示例
- 转载一篇关于.NET 2.0中发送邮件的简单描述