Stmp发送邮件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Net;
using System.Net.Mail;
using System.Net.Mime;
namespace Email
{
public partial class SMTP : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnSend_Click(object sender, EventArgs e)
{
string file = Server.MapPath("testXML.xml");
MailMessage myMail = new System.Net.Mail.MailMessage();
string senderText = txtSender.Text.Trim();//获取或设置此电子邮件的发件人地址
string receiverText = txtReceiver.Text.Trim();//获取或设置此电子邮件的收件人地址
string subjectText = txtSubject.Text.Trim();//获取或设置次电子邮件的主题
string bodyText = txtSender.Text.Trim();//获取或设置邮件正文
myMail = new System.Net.Mail.MailMessage(senderText, receiverText, subjectText, bodyText);
//附件
if (ddlAccessories.Items.Count > 0)
{
for (int i = 0; i < ddlAccessories.Items.Count; i++)
{
System.Net.Mail.Attachment myAttachment = new System.Net.Mail.Attachment(Server.MapPath(ddlAccessories.Items[i].Value), System.Net.Mime.MediaTypeNames.Application.Octet);
System.Net.Mime.ContentDisposition disposition = myAttachment.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
myMail.Attachments.Add(myAttachment);
}
}
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("175.191.40.253", 25);
client.Credentials = new System.Net.NetworkCredential("www.459643912380@qq.com", "w123z123q121q31we21233729281812301");
client.Send(myMail);
}
protected void btnTake_Click(object sender, EventArgs e)
{
if (upFile.PostedFile.ContentLength != 0)
{
string clientFileName = upFile.PostedFile.FileName;
string[] strTemp = clientFileName.Split('.');
string upFileExp = strTemp[strTemp.Length - 1].ToString();
string ServerFileName = DateTime.Now.ToString("yyyyMMddhhmmss") + "." + upFileExp;
upFile.PostedFile.SaveAs(Server.MapPath(ServerFileName));
ddlAccessories.Items.Add(new ListItem(clientFileName, ServerFileName));
if (ddlAccessories.Items.Count > 0)
{
btnDelete.Enabled = true;
}
else
{
btnDelete.Enabled = false;
}
}
}


}
}

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SMTP.aspx.cs" Inherits="Email.SMTP" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
<table align="center" cellpadding="0" cellspacing="0" style=" 755px">
<tr>
<td style="background-image: url(image/标头.jpg); 755px; height: 63px; text-align: center">
</td>
</tr>
</table>
<table align="center" cellpadding="0" cellspacing="0" style=" 755px">
<tr>
<td style="font-size: 9pt; background-image: url(image/链接.jpg); 755px; color: #ffffff;
height: 20px; text-align: right">
◎<asp:HyperLink ID="hpLinkLogin" runat="server" Font-Size="9pt" Font-Underline="False"
ForeColor="White" NavigateUrl="~/Login.aspx">重新登录</asp:HyperLink>
◎<asp:HyperLink ID="hpLinkIndex" runat="server" Font-Size="9pt" Font-Underline="False"
ForeColor="White" NavigateUrl="~/SendEmail.aspx">返回首页</asp:HyperLink>
◎<asp:LinkButton ID="lbtnGroupEmail" runat="server" Font-Size="9pt" Font-Underline="False"
ForeColor="White" >邮件群发</asp:LinkButton>
◎<asp:HyperLink ID="hpLinkAddFriend" runat="server" Font-Size="9pt" Font-Underline="False"
ForeColor="White" NavigateUrl="~/AddFriend.aspx">添加好友</asp:HyperLink>
◎<asp:HyperLink ID="hpLinkFriend" runat="server" Font-Size="9pt" Font-Underline="False"
ForeColor="White" NavigateUrl="~/FriendInfo.aspx">查看好友</asp:HyperLink>&nbsp;
&nbsp; &nbsp;</td>
</tr>
</table>
</div>
<table align="center" cellpadding="0" cellspacing="0" style="background-image: url(image/内容.jpg);
755px; height: 410px">
<tr>
<td style=" 176px; background-color: transparent; text-align: center">
<asp:Label ID="labReceiver" runat="server" Font-Size="9pt" Text="收件人:"></asp:Label>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtReceiver"
ErrorMessage="**"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtReceiver"
ErrorMessage="格式不正确" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator></td>
<td style="font-size: 12pt; 579px; text-align: center" align="left">
<asp:TextBox ID="txtReceiver" runat="server" Font-Size="9pt" Width="575px"></asp:TextBox></td>
</tr>
<tr style="font-size: 12pt">
<td style=" 176px; background-color: transparent; text-align: center">
<asp:Label ID="labSender" runat="server" Font-Size="9pt" Text="发件人:"></asp:Label>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtSender"
ErrorMessage="**"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="txtSender"
ErrorMessage="格式不正确" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator></td>
<td style=" 579px; text-align: center">
<asp:TextBox ID="txtSender" runat="server" Font-Size="9pt" Width="579px"></asp:TextBox></td>
</tr>
<tr style="font-size: 12pt">
<td style=" 176px; background-color: transparent; text-align: center">
<asp:Label ID="labSubject" runat="server" Font-Size="9pt" Text="主题:"></asp:Label>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtSubject"
ErrorMessage="**"></asp:RequiredFieldValidator>
</td>
<td style=" 579px; text-align: center">
<asp:TextBox ID="txtSubject" runat="server" Font-Size="9pt" Width="579px"></asp:TextBox>
</td>
</tr>
<tr style="font-size: 12pt">
<td style=" 176px; background-color: transparent; text-align: center">
<asp:Label ID="labContent" runat="server" Font-Size="9pt" Text="内容:"></asp:Label></td>
<td style=" 579px; text-align: center">
<asp:TextBox ID="txtContent" runat="server" Font-Size="9pt" Height="160px" TextMode="MultiLine"
Width="579px"></asp:TextBox></td>
</tr>
<tr style="font-size: 12pt">
<td style=" 176px; background-color: transparent; text-align: center">
<asp:Label ID="labAccessories" runat="server" Font-Size="9pt" Text="附件:"></asp:Label></td>
<td style=" 579px; text-align: left">
<input id="upFile" runat="server" font-size="9pt" style=" 534px" type="file" />
<asp:Button ID="btnTake" runat="server" Font-Size="9pt" OnClick="btnTake_Click" Text="上传"
Width="40px" CausesValidation="False" /></td>
</tr>
<tr style="font-size: 12pt">
<td style=" 176px; height: 14px; background-color: transparent; text-align: center">
<asp:Label ID="labTakeAcc" runat="server" Font-Size="9pt" Text="已上传附件:"></asp:Label></td>
<td style=" 579px; height: 14px; text-align: left">
<asp:DropDownList ID="ddlAccessories" runat="server" Font-Size="9pt" Width="533px">
</asp:DropDownList>
<asp:Button ID="btnDelete" runat="server" Enabled="False" Font-Size="9pt" Text="删除" Width="40px" /></td>
</tr>
<tr style="font-size: 12pt">
<td style=" 755px; text-align: center">
</td>
<td style=" 755px; height: 35px; text-align: center">
<asp:Button ID="btnSend" runat="server" Font-Size="9pt" OnClick="btnSend_Click" Text="发送"
Width="77px" /></td>
</tr>
</table>

</div>
</form>
</body>
</html>

原文地址:https://www.cnblogs.com/wzq806341010/p/2964050.html