asp.net send Email sample sharing _ Practical Tips

Source: Internet
Author: User
Tags mail account mailmessage net send

Mailhelper-------Mail Help class

Copy Code code as follows:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.Mail;

<summary>
Summary description of Mailhelper
</summary>
public class Mailhelper
{
Public Mailhelper ()
{
//
TODO: Add constructor logic here
//
}

<summary>
Mail Send action
</summary>
<param name= "Addressee" > Recipient address </param>
<param name= "from" > Sender address </param>
<param name= "Sendpassword" > Sender password </param>
<param name= "Copy" > cc person Address </param>
<param name= "Secret" > Secret send Address </param>
<param name= "Subject" > Send theme </param>
<param name= "Attachment" > Attachment information </param>
<param name= "Body" > Message content </param>
public string Sendeemal (string addressee, string from, String Sendpassword, String Copy, String secret, String Subject, St Ring attachment, String body)
{
MailMessage objmailmessage;
MailAttachment objmailattachment;


Create a mail message
Objmailmessage = new MailMessage ();

Sender Email
Objmailmessage.from = from;//Source Email address

Recipient Email
objmailmessage.to = addressee; Destination email Address
CC Message
objmailmessage.cc = Copy;
Mail Misong
OBJMAILMESSAGE.BCC = secret;


Message subject
Objmailmessage.subject = Subject; Send the title of the message

Message content
Objmailmessage.body = body;//Send the contents of the message

Create an Attachment object
if (Attachment!= "")
{
Objmailattachment = new MailAttachment (attachment);//Send message attachment C:\\Test.txt
OBJMAILMESSAGE.ATTACHMENTS.ADD (objmailattachment);//attach attachment to mail message object
}

Then use SMTP to send mail, you need to use the Microsoft. NET Framework SDK v1.1 and its versions above
Basic permissions
OBJMAILMESSAGE.FIELDS.ADD ("Http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
User name
String name = from.substring (0, From.indexof (' @ '));
OBJMAILMESSAGE.FIELDS.ADD ("Http://schemas.microsoft.com/cdo/configuration/sendusername", name);
Password
OBJMAILMESSAGE.FIELDS.ADD ("Http://schemas.microsoft.com/cdo/configuration/sendpassword", Sendpassword);
If you do not have the above three lines of code, you receive the following error message: The server rejected one or more recipient addresses. The server response is: 554:client host rejected:access denied
SMTP address
String smtp = "smtp." + from.substring (From.indexof (' @ ') + 1);
Smtpmail.smtpserver = "smtp." + from.substring (From.indexof (' @ ') + 1);
Start sending mail

Try
{
Smtpmail.send (Objmailmessage);
Return "message sent successfully! ";
}
catch (System.Net.Mail.SmtpException ex)
{
Return ex. message;
}
Core Code End
}
}

And then down was a demo--of his own making

Front desk

Copy Code code as follows:

<%@ Page language= "C #" autoeventwireup= "true" codefile= "Mail.aspx.cs" inherits= "Information_mail"
Validaterequest= "false"%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title></title>
<script src= ". /style/jquery/jquery.js "type=" Text/javascript "></script>
<script src= ". /style/jquery/jquery.validate.js "type=" Text/javascript "></script>
<script language= "javascript" type= "Text/javascript" >

function GEi () {
var file_value = document.getElementById ("File1"). Value;
document.getElementById ("HiddenField1"). Value = File_value;
}
</script>
<body>
<form id= "Form1" runat= "Server" >
<div>
Sent to: <asp:textbox id= "TextBox1" runat= "Server" ></asp:textbox><br/>
CC: <asp:textbox id= "TextBox2" runat= "Server" ></asp:textbox><br/>
Secret send: <asp:textbox id= "TextBox4" runat= "Server" ></asp:textbox><br/>
Topic: <asp:textbox id= "TEXTBOX5" runat= "Server" ></asp:textbox><br/>
Content: <asp:textbox id= "TextBox3" runat= "Server" ></asp:textbox><br/>
Attachment: <input id= "File1" type= "file"/>
<%--<asp:textbox id= "TextBox6" runat= "Server" ></asp:TextBox>--%>
<br/>
<asp:button id= "Button1" runat= "Server" text= "send" onclientclick= "GEi ()" onclick= "Button1_Click"/><br/>
<asp:label id= "Label1" runat= "Server" text= "" ></asp:Label>
</div>
<asp:hiddenfield id= "HiddenField1" runat= "Server"/>
</form>
</body>



Background:
Copy Code code as follows:

protected void Button1_Click (object sender, EventArgs e)
{//instance Mail help class
Mailhelper mails = new Mailhelper ();

string filePath = Hiddenfield1.value;

String a = mails. Sendeemal (TextBox1.Text, "Mail Account", "Mail Password", TextBox2.Text, Textbox4.text, Textbox5.text, FilePath, TextBox3.Text);

Label1.Text = A;
}

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.