Mailhelper ------- mail help class
Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. Mail;
/// <Summary>
/// Summary of mailhelper
/// </Summary>
Public class mailhelper
{
Public mailhelper ()
{
//
// TODO: add the constructor logic here
//
}
/// <Summary>
/// Email sending operation
/// </Summary>
/// <Param name = "Addressee"> recipient address </param>
/// <Param name = "From"> sender address </param>
/// <Param name = "sendpassword"> sender password </param>
/// <Param name = "Copy"> CC address </param>
/// <Param name = "secret"> BCC address </param>
/// <Param name = "Subject"> send topic </param>
/// <Param name = "Attachment"> Attachment Information </param>
/// <Param name = "Body"> email content </param>
Public string SendeEmal (string Addressee, string From, string sendpassword, string Copy, string secret, string Subject, string Attachment, string Body)
{
MailMessage objMailMessage;
MailAttachment objMailAttachment;
// Create an email message
ObjMailMessage = new MailMessage ();
// Sender EMAIL
ObjMailMessage. From = From; // source email address
// Recipient EMAIL
ObjMailMessage. To = Addressee; // target email address
// CC
ObjMailMessage. Cc = Copy;
// Email misong
ObjMailMessage. Bcc = secret;
// Email Subject
ObjMailMessage. Subject = Subject; // the title of the sent Email
// Email content
ObjMailMessage. Body = Body; // content of the sent Email
// Create an attachment object
If (Attachment! = "")
{
ObjMailAttachment = new MailAttachment (Attachment); // The Attachment c: \ test.txt
ObjMailMessage. Attachments. Add (objMailAttachment); // attaches the attachment to the mail message object.
}
// Use SMTP to send emails. You must use Microsoft. NET Framework SDK v1.1 and later versions.
// 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 the preceding three lines of code are not available, the following error message is displayed: the server rejects one or more recipient addresses. Server Response: 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 emails
Try
{
SmtpMail. Send (objMailMessage );
Return "the email is sent successfully! ";
}
Catch (System. Net. Mail. SmtpException ex)
{
Return ex. Message;
}
// The core code ends.
}
}
Then I made a demo by myself --
Front-end
Copy codeThe Code is 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">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
<Script src = "../Style/jquery. js" type = "text/javascript"> </script>
<Script src = "../Style/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>
</Head>
<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/>
BCC: <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 = "button#click"/> <br/>
<Asp: Label ID = "Label1" runat = "server" Text = ""> </asp: Label>
</Div>
<Asp: HiddenField ID = "HiddenField1" runat = "server"/>
</Form>
</Body>
</Html>
Background:
Copy codeThe Code is as follows:
Protected void button#click (object sender, EventArgs e)
{// Instance email help class
Mailhelper mails = new mailhelper ();
String filePath = HiddenField1.Value;
String a = mails. SendeEmal (TextBox1.Text, "email account", "email password", TextBox2.Text, TextBox4.Text, TextBox5.Text, filePath, TextBox3.Text );
Label1.Text =;
}