Use Gmail to send mail in. Net

Source: Internet
Author: User
Tags log mailmessage server port smtpclient
gmail| Send mail

In project development, a very common function is to send mail. Under normal circumstances, large companies have their own mail system, we can directly through the company's POP/SMTP server to send and receive mail. However, for some small companies that do not have such conditions, they generally through a number of public mail services through the provider of mail services. For example, sina,163 is a good, commonly used mail service. But by contrast, I'm still used to Google Gmail.


First, send mail through Gmail in. Net

We know that SMTP is our most common protocol for message transfer. Through SMTP, we only need to configure the corresponding STMP server and port, using our account and password to log into the stmp Server, in theory we can send the mail. For Google Gmail, the corresponding information is as follows:

POP3 Server (port:995): pop.gmail.com, SSL


SMTP Server (port:25, 465, 587): smtp.gmail.com, TLS

You can log in to smtp.gmail.com by registering your Gmail account number and password. The following is a simple C # Code.


Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Net.Mail;
Using System.Net;

Namespace Artech.Mail.ConsoleApp
{
Class Program
{

        const string address_from = "from@gail.com";
        const string address_to = "to@gmail.com";
        const string user_id = "MyAccount";
        const string PASSWORD = "PASSWORD";
        const string smtp_server = "smtp.gmail.com";
        const int PORT = 587;
 
        static void Main (string[] args)
         {


SendMail (Smtp_server, PORT);
Console.read ();

}

static void SendMail (string smtpserver, int port)


{
SmtpClient mailclient = new SmtpClient (SmtpServer, 587);
Mailclient.enablessl = true;
NetworkCredential crendetial = new NetworkCredential (user_id, PASSWORD);

Mailclient.credentials = crendetial;
MailMessage message = new MailMessage (Address_from, address_to, ' This is a subject ', ' This is the ' body of the Mail ');

Mailclient.send (message);
Console.WriteLine ("Mail has been sent to ' {0} '", address_to);
}
}
}

Familiar with System.Net.Mail SmtpClient, the code above should be very familiar with, here I do not want to do the above logic to introduce more. But I need to add a few points:

1. With Gmail, you can only send a letter in the name of the account you log in to SMTP server, as an example, I log in as "MyAccount" most of Gmail, and email address Send mail for to@gmail.com, although in the Smtpclient.send method I specify the From address as from@gail.com, when the recipient receives the message, the sender of the message is myaccount@ Gail.com, not for from@gail.com. These are necessary to prevent you from using someone else's name to send mail. The mechanism is not universal, and I have joked with my colleagues that I emailed him through the company's STMP server in the name of another colleague.

2. Although Google claims that they have developed SMTP server port 25,465 and 587, in the code, I use 25 and 5,871 tangent Normal, when I use 465, how can not send out. But when I configure Port to be 465 in Outlook, sending mail is OK. I haven't had time to check what the problem is. Those who know the reason, please do not hesitate to enlighten me.

3. For code like this mail service function, we generally write configurable. Because for accounts and passwords, and even STMP servers, it is possible to change them frequently. But we do not have to use the common <AppSettings> to configure, do not define our custom ConfigurationSection. Because configuration system has configured the message-related information for the built-in <mailSettings> that we have defined. Like what:

<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<system.net>
<mailSettings>
<SMTP from= "MyAccount@gmail.com" >
<network host= "Smtp.gmail.com"
password= "Password"
Port= "587"
Username= "MyAccount @gmail. com"/>
</smtp>
</mailSettings>
</system.net>
</configuration>

It doesn't really mean anything to gmail,from.

Now we can further simplify our managed code:


static void SendMail ()
{

SmtpClient mailclient = new SmtpClient ();
Mailclient.enablessl = true;
MailMessage message = new MailMessage (Address_from, address_to, ' This is a subject ', ' This is the ' body of the Mail ');

Mailclient.send (message);
Console.WriteLine ("Mail has been sent to ' {0} '", address_to);
}



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.