asp.Net 163メール送信
10151 ワード
<table id="TABLE1" runat="server" border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="width: 393px">
:<asp:TextBox ID="TextBox1" runat="server">kally32@163.com</asp:TextBox><br />
:<asp:TextBox ID="TextBox2" runat="server"> </asp:TextBox><br />
:<asp:TextBox ID="TextBox3" runat="server" Height="154px" TextMode="MultiLine"
Width="336px"> </asp:TextBox><br />
<asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text=" " />
</td>
</tr>
</table>
</div>
<table id="Table2" runat="server" border="0" cellpadding="0" cellspacing="0" visible="false">
<tr>
<td align="center" style="width: 400px">
<asp:Label ID="Label1" runat="server" ForeColor="Red" Text=" , !"></asp:Label><br />
<asp:Button ID="Button2" runat="server" Text=" " onclick="Button2_Click" />
</td>
</tr>
</table>
以上がフロントコード
以下はバックグラウンドコードです
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;
namespace WebTestMail
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
// ,
Response.Redirect(Request.Url.ToString());
TABLE1.Visible = true;
Table2.Visible = false;
}
protected void Button3_Click(object sender, EventArgs e)
{
// (SMTP、POP3) 、
//http://blog.sina.com.cn/s/blog_6e85b10501012kyv.html
//smtp.163.com
bool reVal = SendMail("smtp.163.com", 25, "", "", " ", TextBox2.Text, TextBox3.Text, "");
if (reVal) {
Label1.Text = " ! 。";
}
else
{
Label1.Text = " , 。";
}
TABLE1.Visible = false;
Table2.Visible = true;
}
//
/*
* strSmtpServer:
* iSmtpPort:
* Password:
* strFrom:
* strto:
* strSubject:
* strBody:
*/
public bool SendMail(string strSmtpServer, int iSmtpPort, string Password, string strFrom, string strto, string strSubject, string strBody, string strFileName)
{
// ,
MailAddress mailFrom = new MailAddress(strFrom);
// ,
MailAddress mailTo = new MailAddress(strto);
// MailMessage
MailMessage oMail = new MailMessage(mailFrom, mailTo);
try
{
oMail.Subject = TextBox2.Text; //
oMail.Body = TextBox3.Text; //
oMail.IsBodyHtml = true; // , HTML
oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");//
oMail.SubjectEncoding = System.Text.Encoding.GetEncoding("GB2312");//
oMail.Priority = MailPriority.High;//
//
//System.Web.Mail.MailAttachment mailAttachment=new System.Web.Mail.MailAttachment(@"f:/baihe.txt");
if (strFileName != "" && strFileName != null)
{
Attachment data = new Attachment(strFileName);
oMail.Attachments.Add(data);
}
//
SmtpClient client = new SmtpClient();
// smtp
//
client.Host = strSmtpServer; //
//
client.Port = iSmtpPort;
//
client.Timeout = 9999;
//
client.UseDefaultCredentials = true;
// ,
//
client.Credentials = new NetworkCredential(strFrom, Password);
client.Send(oMail); //
//
mailFrom = null;
mailTo = null;
client.Dispose();//
oMail.Dispose(); //
return true;
}
catch (Exception ex)
{
//
mailFrom = null;
mailTo = null;
oMail.Dispose(); //
return false;
}
}
}
}