asp.Net添付機能付きの簡単なメール送信
3983 ワード
<div>
:<asp:TextBox ID="txt_toman" runat="server"></asp:TextBox>
<br />
:<asp:TextBox ID="txt_title" runat="server"></asp:TextBox>
<br />
:<asp:TextBox ID="txt_content" runat="server" Height="66px"
TextMode="MultiLine" Width="194px"></asp:TextBox>
<br />
:<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
<asp:Button ID="btn_send" runat="server" onclick="btn_send_Click"
Text=" Send " />
<asp:Label ID="lbl_mag" runat="server" ForeColor="Red"></asp:Label>
</div>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Text;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_send_Click(object sender, EventArgs e)
{
bool flag = SendMail(" ", " ", " ", " ", txt_title.Text, txt_content.Text, txt_toman.Text, FileUpload1);
if (flag)
{
lbl_mag.Text = " ";
}
else
{
lbl_mag.Text = " , !";
}
}
/// <summary>
///
/// </summary>
/// <param name="user"> </param>
/// <param name="who"> </param>
/// <param name="name"> </param>
/// <param name="pwd"> </param>
/// <param name="title"> </param>
/// <param name="body"> </param>
/// <param name="shoujian"> </param>
/// <param name="file"> </param>
/// <returns> </returns>
public static bool SendMail(string user, string who, string name, string pwd, string title, string body, string shoujian, FileUpload file)
{
MailMessage Message = new MailMessage(
new MailAddress(user, // ,
who, //
Encoding.UTF8), //
new MailAddress(shoujian));//
if (file.HasFile)
{
//
if (file.PostedFile != null)
{
Attachment attachment = new Attachment(file.PostedFile.InputStream, file.PostedFile.FileName);
Message.Attachments.Add(attachment);
}
}
Message.SubjectEncoding = Encoding.UTF8;
Message.Subject = title;//
Message.BodyEncoding = Encoding.UTF8;
Message.IsBodyHtml = true;
Message.Body = body; //
Message.Priority = MailPriority.High;
SmtpClient smtpClient = new SmtpClient("smtp.163.com", 25);
// //
smtpClient.Credentials = new System.Net.NetworkCredential(name, pwd);
// //
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Timeout = 99999;
try
{
smtpClient.Send(Message);
return true;
}
catch (Exception)
{
return false;
}
}
}
}