Week Four: Java for mail sending

Source: Internet
Author: User

How to implement the Java Send mail function

1. First download the local mail-sending software so that the test

1). After installing MailServer, add two local users, as follows

  

2). After installing Foxmail, add two users as before, and the user name is the same, use Foxmail to send and receive mail conveniently. Note: You must add users to the mailserver before sending, and the user name is consistent. Also as follows:

To be filled in as local when configured

2. Import the appropriate package

3. Use Java to implement email delivery

The code is as follows:

Import java.util.Properties;

Import Javax.mail.Message;
Import javax.mail.MessagingException;
Import javax.mail.Session;
Import Javax.mail.Transport;
Import Javax.mail.Message.RecipientType;
Import javax.mail.internet.AddressException;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeMessage;

public class MailDemo1 {
public static void Main (string[] args) throws exception{
Properties prop = new properties ();
Prop.setproperty ("Mail.transport.protocol", "SMTP");//protocol
Prop.setproperty ("Mail.smtp.host", "localhost");//Host Name
Prop.setproperty ("Mail.smtp.auth", "true");//whether permission control is turned on
Prop.setproperty ("Mail.debug", "true");//If set to True the message will be printed when the message is sent
Create a session between a program and a mail server
Session session = Session.getinstance (prop);
Get Mail Object
Message msg = new MimeMessage (session);
Msg.setfrom (New InternetAddress ("[email protected]");
Msg.setrecipients (recipienttype.to, New internetaddress[]{new internetaddress ("[email protected]")});
Msg.setsubject ("This is an email from a Java program");
Msg.settext ("This is the body of the message ... Content .... ");
Enter your account password
Transport trans = Session.gettransport ();
Trans.connect ("AA", "123");
Trans.sendmessage (MSG, msg.getallrecipients ());
}
}

Fourth Week job: Java implementation mail send

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.