<Div>
Recipient: <asp: TextBox ID = "txt_toman" runat = "server"> </asp: TextBox>
<Br/>
Topic: <asp: TextBox ID = "txt_title" runat = "server"> </asp: TextBox>
<Br/>
Content: <asp: TextBox ID = "txt_content" runat = "server" Height = "66px"
TextMode = "MultiLine" Width = "194px"> </asp: TextBox>
<Br/>
Attachment: <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>
View plain
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 ("sender address", "sender", "User Name", "password", txt_title.Text, txt_content.Text, txt_toman.Text, FileUpload1 );
If (flag)
{
Lbl_mag.Text = "sent successfully ";
}
Else
{
Lbl_mag.Text = "poor, failed to send! ";
}
}
/// <Summary>
/// Simple email sender
/// </Summary>
/// <Param name = "user"> sender address </param>
/// <Param name = "who"> sender </param>
/// <Param name = "name"> sending user name </param>
/// <Param name = "pwd"> user name and password </param>
/// <Param name = "title"> email title </param>
/// <Param name = "body"> sent content </param>
/// <Param name = "shoujian"> recipient address </param>
/// <Param name = "file"> attachment </param>
/// <Returns> whether the message is sent successfully </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, // The first is the sender's address,
Who, // The second parameter is the sender
Encoding. UTF8), // Encoding
New MailAddress (shoujian); // recipient's email address
If (file. HasFile)
{
// Add an attachment
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; // title
Message. BodyEncoding = Encoding. UTF8;
Message. IsBodyHtml = true;
Message. Body = body; // subject
Message. Priority = MailPriority. High;
SmtpClient smtpClient = new SmtpClient ("smtp.163.com", 25 );
// Email server // sending Port
SmtpClient. Credentials = new System. Net. NetworkCredential (name, pwd );
// User name and password
SmtpClient. DeliveryMethod = SmtpDeliveryMethod. Network;
SmtpClient. Timeout = 99999;
Try
{
SmtpClient. Send (Message );
Return true;
}
Catch (Exception)
{
Return false;
}
}
}
}
From Sweet Life, You Can Be calm, calm, and competitive. Let it go!