implementation send mail

Source: Internet
Author: User
Tags base64 mailmessage smtpclient

private void Page_Load (object sender, System.EventArgs e)
String Sreturn = String. Empty;
MailMessage MAILMSG = new MailMessage ();
Mailmsg.bodyformat = mailformat.html; = "****";
Mailmsg.from = "***";
Mailmsg.subject = "email." Subject ";
Mailmsg.body = "email." Body ";

MAILMSG.FIELDS.ADD ("Http://", "1");
MAILMSG.FIELDS.ADD ("Http://", "User name");
MAILMSG.FIELDS.ADD ("Http://", "password");
Smtpmail.smtpserver = "";//smpt server

Smtpmail.send (MAILMSG);
This. Label1.Text = "Send Success";
catch (Exception err)
This. Label2.Text = "<font color=red> send failed" + Err. Message.tostring () + "</font>";

Respondents: Soccer 10th-thousand total four 5-28 13:57
The questioner's assessment of the answer:
Thank you for the information you sent me.
The following is my own written test email:
Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Net;
Using System.Net.Mail;

Namespace Logic
public class Mail
public bool Servicesendmail (string Strto, String strsubject, String strbody, String strencoding)
Return Common (Strto, "", Strsubject, Strbody, strencoding);

///mail Delivery (service mailbox), default encoding is GB2312
///<param name= "Strto" > </param>
///<param name= "STRCC" ></param>
///<param name= "Strsubject" ></param >
///<param name= "strbody" ></param>
///<param name= "strencoding" > Encoding, if NULL, default to GB2312 </param>
public bool Servicesendmail (string Strto, String STRCC, String strsubject, String strbody, String strencoding)
Return common (Strto, STRCC, Strsubject, Strbody, strencod ing);

Mail sends default encoding for GB2312
<param name= "Strto" ></param>
<param name= "Strsubject" ></param>
<param name= "Strbody" ></param>
public bool Servicesendmail (string Strto, String strsubject, String strbody)
Return Common (Strto, "", Strsubject, Strbody, "");

private static bool Common (string Strto, String STRCC, String strsubject, String strbody, String strencoding)
BOOL Bstate = false;

The code is rigidly defined as GB2312
Encoding Encoding = encoding.getencoding (936);

MailMessage message = new MailMessage (
New MailAddress ("", "hanjunhui127", encoding),
New MailAddress (Strto));

message.subjectencoding = encoding;
Message.subject = Strsubject;
message.bodyencoding = encoding;
Message.body = strbody;
if (STRCC!= "")
MESSAGE.CC.ADD (New MailAddress (STRCC));
SmtpClient smtpclient = new SmtpClient ("");
Smtpclient.credentials = new NetworkCredential ("hanjunhui127", "1984127");
Smtpclient.timeout = 999999;
Smtpclient.send (message);

Bstate = true;

catch (Exception ex)
throw new Exception (ex. message);

return bstate;


You compile the above code into a DLL, referencing that DLL in a Web application
You can write that in Aspx.cs.
Using System;
Using System.Data;
Using System.Configuration;
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 Logic;
public partial class
protected void Page_Load (object sender, EventArgs e)
Logic.mail m = new Mail ();
M.servicesendmail ("", "Hello", "I'm Good" "," "" ");
Response.Write ("Success!");

I've tested it successfully.
If not, please continue to refer to the following article
You can refer to these two articles first:

Respondents: H1J2H7-wizard five level 5-20 02:06
? .....
answered by:
Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Net;
Using System.Net.Mail;
Using Dbmodel;

Namespace MailSender
public class Mailutil
Send #region e-mail

public bool SendMail (Mailinfo mailinfo)
/* Sender's information * *
string smtp = Mailinfo.mailstmp; The server of the mailbox used by the sender
string mailform = Mailinfo.mailfrom; Sender's mailbox
string mailpwd = Mailinfo.mailpwd; Sender's password
string mailTo = Mailinfo.mailto; Recipient information
string mailtitle = Mailinfo.mailtitle; Message headers
string mailcontent = Mailinfo.mailcontent; Message content

SmtpClient client = new SmtpClient (SMTP);
Client. useDefaultCredentials = false;
Client. Credentials = new NetworkCredential (Mailform, mailpwd);
/* Specify how to process outgoing messages * *
Client. Deliverymethod =;

MailMessage message = new MailMessage (Mailform, MailTo, Mailtitle, mailcontent);
Message. bodyencoding = Encoding.default;
Message. Isbodyhtml = true;

Client. Send (message);
return true;
catch (Exception ex)
return false;


Tested and useful (Mailinfo is an entity for saving user information)
answered by:
Partial Class Mailset
Inherits System.Web.UI.Page
Protected Sub button1_click (ByVal sender as Object, ByVal e as System.EventArgs) Handles Button1.Click
Dim Client as New smtpclient ("", 25)
Client. Deliverymethod =
Client. Credentials = New System.Net.NetworkCredential ("Hu Jintao", "Hu Jintao")
Dim msg as MailMessage
msg = New MailMessage ("******", "******", "Hello", "Hello")
Msg. isbodyhtml = True
Client. Send (msg)
End Sub
End Class
answered by:
It's with JMail.

Private Sub Btnok_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles
If SendMail () Then
Label1.Text = "message sent successfully. "
Label1.cssclass = "Red"
End If
End Sub

Function SendMail () as Boolean
Dim smtpserver as String = ConfigurationSettings.AppSettings ("SmtpServer")
Dim Smtpuser as String = ConfigurationSettings.AppSettings ("Smtpuser")
Dim Smtppass as String = ConfigurationSettings.AppSettings ("Smtppass")

Dim result as Boolean
Dim Ojmail as New jmail. MessageClass
Ojmail.charset = "gb2312"
ojmail.encoding = "Base64"
ojmail.contenttransferencoding = "Base64"
' Ojmail.contenttype = ' text/html '
Ojmail.isoencodeheaders = False
Ojmail.priority = Convert.tobyte (3)

Ojmail.from = Myemail.text
Ojmail.fromname = Request.Cookies ("mycookies") ("MyName")
Ojmail.subject = Regex.Replace (Title.text, "<[^>]+>", "")
Ojmail.addrecipient (Email.text)
Ojmail.mailserverusername = Smtpuser
Ojmail.mailserverpassword = Smtppass

' Ojmail.body = Request ("Content")
Ojmail.appendhtml (Request ("Content"). ToString)

' Traverse the file Form element
Dim files as System.web.HttpFileCollection = System.Web.HttpContext.Current.Request.Files
Dim IFile as Int32
For ifile = 0 to files. Count-1
' Check file extension name
Dim PostedFile as System.Web.HttpPostedFile = Files (ifile)
Dim filename as String = postedFile.FileName.ToString
Dim ftype as String = postedFile.ContentType.ToString
Dim filesize as Integer = Postedfile.contentlength
Dim soriginalfilename = System.IO.Path.GetFileName (filename)
Dim Sfileext as String = System.IO.Path.GetExtension (soriginalfilename)
If filename <> "" Then
Ojmail.addattachment (filename, False, filename.) Substring (filename. LastIndexOf (".") + 1, 3)
End If
Catch Ex as System.Exception
Label1.cssclass = "Red"
Label1.Text = Ex.message
Return False
End Try

If ojmail.send (SmtpServer, False) Then
result = True
result = False
End If
Ojmail.close ()
Ojmail = Nothing
return result
End Function
answered by:
answered by:
Ocean X-Senior manager level seven 5-26 23:00
' Argument title is the title, Boby content, towho to send to the mail address, sent successfully returned empty, send failure return error message
Vb. NET practices
Public Function SendMail (ByVal title As String, ByVal body As String, ByVal towho as String) as String
Dim MSG as New mailmessage
Msg.from = "Your email address" = towho
Msg.subject = Title
Msg.bodyformat = mailformat.html
msg.bodyencoding = System.Text.Encoding.Default
Msg.priority = Mailpriority.high
Msg.body = Body
Http:// "," 1 ")
MSG.FIELDS.ADD ("Http://", "Your email login name")
MSG.FIELDS.ADD ("Http://", "Your email login address")
System.Web.Mail.SmtpMail.SmtpServer = "Mail server address, such as"
System.Web.Mail.SmtpMail.Send (MSG)
SendMail = ""
Catch ex as Exception
SendMail = ex. Message
End Try
End Function
C # practices
public string SendMail (string title,string body,string towho)
MailMessage msg=new MailMessage ();
Msg.from = "Your email address"; = towho;
Msg.subject = title;
Msg.bodyformat = mailformat.html;
msg.bodyencoding = System.Text.Encoding.Default;
msg.priority = Mailpriority.high;
Msg.body = body;
MSG.FIELDS.ADD ("Http://", "1");
MSG.FIELDS.ADD ("Http://", "Your email login name");
MSG.FIELDS.ADD ("Http://", "Your email login address");
System.Web.Mail.SmtpMail.SmtpServer = "Mail server address, such as"
System.Web.Mail.SmtpMail.Send (MSG)
Return "";
catch (Exception exp)
Return ex. message;


The article is reproduced from the network base:

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: 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.