C # mail sending-sendmail

Source: Internet
Author: User
Tags mailmessage
Sendmail. aspx

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "sendmail. aspx. cs" inherits = "admin_sendmail" %>

<! 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 id = "head1" runat = "server">
<Title> sendmail </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Table border = "0" align = "center" cellpadding = "0" cellspacing = "5">

<Tr>
<TD width = "100"> recipient: </TD>
<TD width = "600"> <asp: textbox id = "addressee" runat = "server" width = "600"> </ASP: textbox>
<Asp: requiredfieldvalidator id = "requiredfieldvalidator1" runat = "server" errormessage = "addressee can't be empty! "Controltovalidate =" subject "font-bold =" true"
Display = "dynamic" forecolor = "black"> </ASP: requiredfieldvalidator>
<Asp: regularexpressionvalidator id = "re" runat = "server" errormessage = "The format inaccuracy of email! "Controltovalidate =" addressee "font-bold =" true"
Validationexpression = "\ W + ([-+.] \ W +) * @ \ W + ([-.] \ W + )*\. \ W + ([-.] \ W +) * "forecolor =" black "> </ASP: regularexpressionvalidator> </TD>
</Tr>
<Tr>
<TD> CC: </TD>
<TD> <asp: textbox id = "copy" runat = "server" width = "600"> </ASP: textbox> </TD>
</Tr>
<Tr>
<TD> BCC: </TD>
<TD> <asp: textbox id = "secret" runat = "server" width = "600"> </ASP: textbox> </TD>
</Tr>
<Tr>
<TD> topic: </TD>
<TD> <asp: textbox id = "subject" runat = "server" width = "600"> </ASP: textbox>
<Asp: requiredfieldvalidator id = "Ru" runat = "server" errormessage = "subject can't be empty! "Controltovalidate =" subject "font-bold =" true"
Display = "dynamic" forecolor = "black"> </ASP: requiredfieldvalidator> </TD>
</Tr>
<Tr>
<TD> attachment: </TD>
<TD> <asp: textbox id = "attachment" runat = "server" width = "600"> </ASP: textbox> </TD>
</Tr>
<Tr>
<TD> body: </TD>
<TD> <asp: textbox id = "body" runat = "server" textmode = "multiline" rows = "18" width = "600"> </ASP: textbox> </TD>
</Tr>
<Tr>
<TD colspan = "2" align = "center">
<Asp: button id = "send" runat = "server" text = "send" onclick = "sendmailusegmail"/> & nbsp; & nbsp;
<Asp: button id = "refurbish" runat = "server" text = "refurbish" onclick = "refurbish_click"> </ASP: button> </TD>
</Tr>
</Table>
</Form>
</Body>
</Html>


 

Sendmail. aspx. CS

Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Web. Mail; // CoreCodeStart
Using system. Windows. forms;

// No debugging is available below

Public partial class admin_sendmail: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{

}

Public bool addrecipient (string Str) // CC recipient
{
STR = Str. Trim ();
If (STR = NULL | STR = "" | Str. indexof ("@") =-1)
Return true;
If (recipientnum <10)
{
Recipient. Add (recipientnum, STR );
Recipientnum ++;
Return true;
}
Else
{
Errmsg + = "too many recipients ";
Return false;
}
}


Public bool addrecipientbcc (string Str) // BCC recipient
{
If (STR = NULL | Str. Trim () = "")
Return true;
If (recipientbccnum <10)
{
Recipientbcc. Add (recipientbccnum, STR );
Recipientbccnum ++;
Return true;
}
Else
{
Errmsg + = "too many recipients ";
Return false;
}
}

Public void sendmailusegmail (Object sender, eventargs E)
{
Mailmessage objmailmessage;
Mailattachment objmailattachment;

// Create an email message
Objmailmessage = new mailmessage ();

// Sender email
Objmailmessage. From = "ujtrading@ujtrading.com"; // source email address

// Recipient email
String addressee = This. addressee. text;
Objmailmessage. To = addressee; // target email address
// Objmailmessage. To = "shirley@ujtrading.com ";

// Email Subject
String subject = This. Subject. text;
Objmailmessage. Subject = subject; // the title of the sent Email
// Objmailmessage. Subject = "test ";

// Email content
String body = This. Body. text;
Objmailmessage. Body = body; // content of the sent Email

// Create an attachment object
String attachment = This. Attachment. text;
If (this. Attachment. Text. tostring ()! = "")
{
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
Objmailmessage. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/sendusername", "ujtrading ");
// Password
Objmailmessage. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/sendpassword", "789456 ");
// 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
Smtpmail. smtpserver = "smtp.ujtrading.com ";
// Start sending emails

Try
{
Smtpmail. Send (objmailmessage );
// You can simply use client. Send (MSG );
MessageBox. Show ("send mail success! "," Messige ");
This. addressee. Text = "";
}
Catch (system. net. Mail. smtpexception ex)
{
MessageBox. Show (ex. message, "Send mail fail! ");
}
// The core code ends.
}
Protected void refurbish_click (Object sender, eventargs E)
{
This. addressee. Text = "";
This. Subject. Text = "";
This. Body. Text = "";
This. Attachment. Text = "";
}
}

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.