C # send mail with gmail email __c#

Source: Internet
Author: User
Tags datetime ming mailmessage smtpclient

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;
Using System.Net.Mail;

Namespace WindowsFormsApplication1
{
public partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
}

private void Button1_Click (object sender, EventArgs e)
{
Sendsmforgmail ("ouyjpem@gmail.com", "Akdkdls", "Li Ming", "Test", "good recently." "," 13577221209@139.com ");
}


private string Sendsmforgmail (string sender, String pwd, string name, String smtitle, String smcontent, string receiver)
{
Example Sendsmforgmail ("Liming@gmail.con", "Li Ming", "Test", "recent good.") "," limei@163.com ")
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage ();
Msg. To.add (PhoneNumber + "@139.com");
Msg. To.add (receiver);
Msg. To.add ("");
Msg. To.add ("b@b.com");
/*
* Msg. To.add ("b@b.com");
* Msg. To.add ("b@b.com");
* Msg. To.add ("b@b.com"); can be sent to many people
*/
Msg. Cc. Add ("CC address");
/*
* Msg. Cc. ADD ("c@c.com");
* Msg. Cc. ADD ("c@c.com"); can be copied to many people
*/
Msg. from = new MailAddress (sender, Name, System.Text.Encoding.UTF8);
* * Above 3 parameters are the sender's address (can be casually written), sender name, code * *
Msg. Subject = smtitle;//message Header
Msg. subjectencoding = system.text.encoding.utf8;//message header encoding
Msg. BODY = smcontent;//message Content
Msg. bodyencoding = system.text.encoding.utf8;//message content encoding
Msg. isbodyhtml = false;//is an HTML message
Msg. Priority = mailpriority.high;//Message priority

SmtpClient client = new SmtpClient ();
Client. Credentials = new System.Net.NetworkCredential (sender, PWD);
Write your email and password above
Client. Port = Ports Used by 587;//gmail
Client. Host = "smtp.gmail.com";
Client. Enablessl = true;//after SSL encryption
Object userState = msg;
Try
{
Client. SendAsync (msg, userState);
Client. Send (msg);
Just a little bit easier to be a client. Send (msg);
MessageBox.Show ("Send Success");
TextBox2.Text = TextBox2.Text + DateTime.Now.ToString () + "Have sent a message!" + "/r/n";
return "1";
}
catch (System.Net.Mail.SmtpException ex)
{
MessageBox.Show (ex. Message, "Send mail Error");
TextBox2.Text = TextBox2.Text + DateTime.Now.ToString () + "" + Ex. Message + "/r/n";
return "0";
}
}
}
}

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.