Asp.net simple email sending with attachment Function

Source: Internet
Author: User
Tags mailmessage smtpclient

 

<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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.