asp.net implementation to send mail (from Baidu know) __.net

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.to = "****@163.com";
Mailmsg.from = "***@163.com";
Mailmsg.subject = "email." Subject ";
Mailmsg.body = "email." Body ";

MAILMSG.FIELDS.ADD ("Http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
MAILMSG.FIELDS.ADD ("Http://schemas.microsoft.com/cdo/configuration/sendusername", "User name");
MAILMSG.FIELDS.ADD ("Http://schemas.microsoft.com/cdo/configuration/sendpassword", "password");
//
Smtpmail.smtpserver = "smtp.126.com";//smpt server
Try
{

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


Respondents: Football No. 10th-thousand four level 5-28 13:57 the questioner's comment on the answer: Thank you for the information I sent. The following is a test email that I personally wrote:
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);
}

<summary>
Mail Send (service mailbox), default encoding is GB2312
</summary>
<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>
<returns></returns>
public bool Servicesendmail (string Strto, String STRCC, String strsubject, String strbody, String strencoding)
{
Return Common (Strto, STRCC, Strsubject, Strbody, strencoding);
}

<summary>
Mail sends default encoding for GB2312
</summary>
<param name= "Strto" ></param>
<param name= "Strsubject" ></param>
<param name= "Strbody" ></param>
<returns></returns>
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;

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

MailMessage message = new MailMessage (
New MailAddress ("hanjunhui127@sina.com", "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 ("smtp.sina.com");
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 _default:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
Logic.mail m = new Mail ();
M.servicesendmail ("hjh127@163.com", "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:
Http://www.5iaspx.com/aspnet/C-XieDeQiChi-SMTP-YanZhengDeFaSongYouJianQuJian-1-peq20134.html
Http://www.5iaspx.com/aspnet/C-XieDeQiChi-SMTP-YanZhengDeFaSongYouJianQuJian-2-ci9k0137.html

Reference: http://www.5iaspx.com 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)
{
Try
{
/* 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 = Smtpdeliverymethod.network;

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;
}
}

#endregion
}
}



Tested and useful (Mailinfo is an entity for saving user information)
answered by: Imports System.net.Mail
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 ("smtp.163.com", 25)
Client. Deliverymethod = Smtpdeliverymethod.network
Client. Credentials = New System.Net.NetworkCredential ("Hu Jintao", "Hu Jintao")
Dim msg as MailMessage
msg = New MailMessage ("******@163.com", "******@163.com", "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 Btnok.click
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
Try
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
Next
Catch Ex as System.Exception
Label1.cssclass = "Red"
Label1.Text = Ex.message
Return False
End Try

If ojmail.send (SmtpServer, False) Then
result = True
Else
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"
Msg.to = 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://schemas.microsoft.com/cdo/configuration/smtpauthenticate "," 1 ")
MSG.FIELDS.ADD ("Http://schemas.microsoft.com/cdo/configuration/sendusername", "Your email login name")
MSG.FIELDS.ADD ("Http://schemas.microsoft.com/cdo/configuration/sendpassword", "Your email login address")
Try
System.Web.Mail.SmtpMail.SmtpServer = "Mail server address, such as smtp.126.com"
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";
msg.to = 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://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
MSG.FIELDS.ADD ("Http://schemas.microsoft.com/cdo/configuration/sendusername", "Your email login name");
MSG.FIELDS.ADD ("Http://schemas.microsoft.com/cdo/configuration/sendpassword", "Your email login address");
Try
{
System.Web.Mail.SmtpMail.SmtpServer = "Mail server address, such as smtp.126.com"
System.Web.Mail.SmtpMail.Send (MSG)
Return "";
}
catch (Exception exp)
{
Return ex. message;
}

} http://www.51aspx.com/CV/JH1GEZ9ACZA70/

Can send mail, choose whether HTML format such as LIUJIAYU10-Lake rookie level four 5-26 08:49 xuyue3000-apprentice magician level two 5-25 19:27 Soar_gh-Probation level 5-25 10:42 sdn110-Assistant Second Level 5-20 10:23
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.