Java wheel-java send QQ mail __java

Source: Internet
Author: User
Tags gettext imap

To send a message, you first have to figure out what the stack of different protocols are. The following paragraph refers to NetEase


POP3

POP3 is the Post Office Protocol 3 abbreviation, the 3rd version of the Post Office Protocol, which provides a way to connect personal computers to the Internet's mail server and electronic protocols for downloading e-mail.

It is the first offline protocol standard for Internet e-mail, POP3 allows users to store messages from a server on a local host (that is, their own computer) while deleting messages that are saved on the mail server, and the POP3 server follows the POP3 protocol's receiving mail server to receive e-mail.

SMTP

The full name of SMTP is "Simple Mail Transfer Protocol", which is simply a message transfer protocol. It is a set of specifications for transmitting messages from the source address to the destination to control how the message is relayed. The SMTP protocol belongs to the TCP/IP protocol cluster, which helps each computer find the next destination when sending or relaying letters. The SMTP server is the outgoing mail server that follows the SMTP protocol.

SMTP authentication, simply by requiring that you have an account name and password before you can log on to the SMTP server, makes it easy for those spammers to do so. The purpose of increasing SMTP authentication is to protect users from spam.

IMAP

The IMAP full name is the Internet Mail Access Protocol, the interactive Mail Access Protocol, which is one of the standard protocols for similar mail access to POP3. On the other hand, when you turn on IMAP, the messages you receive from the email client remain on the server, and the actions on the client are fed back to the server, such as deleting the message, marking the read, and so on, and the message on the server will act accordingly. So whether you log in to the mailbox from the browser or the client software login mailbox, see the message and the status is consistent.


This article is mainly based on the SMTP protocol to do the sending of QQ mail. As you can see from the above introduction, there are three things you need to send an email to SMTP:

1. Email address of sender.

2. The password of the sender's SMTP mailbox, this password is not the password of the mailbox, you login to QQ mailbox, find the SMTP function, open, will give you generate a, this later again.

3. Address of the Receiving party

1 and 3 are easier to get, 2 of the acquisition, first you need to login QQ mailbox, and then click Settings-General-account, open the SMTP service, and then click the Generate authorization code, you can get 2 said password.


Well, you can write code by getting these.

First, in my code, write two Pojo classes, which are mail class and mailbox class, mail class set the content title of the message and the destination email address you want to send. The mailbox class sets some properties of the mailbox, such as the type of protocol used, user name, password, and so on. This can be read from the configuration file using spring boot, and for simplicity, this is not the case. Write it to death directly. The following is the Pojo class code.


Package com.xxjz.sendemail;


Import java.io.Serializable;

public class Mail implements Serializable {
	private static final long serialversionuid = -543077416549260588l;
	Private String SendTo;
	Private String subject;
	private String text;
	Public String Getsendto () {return
		sendTo;
	}
	public void Setsendto (String sendTo) {
		this.sendto = sendTo;
	}
	Public String Getsubject () {return
		subject;
	}
	public void Setsubject (String subject) {
		this.subject = subject;
	}
	Public String GetText () {return
		text;
	}
	public void SetText (String text) {
		this.text = text;
	}
	Public Mail () {
		super ()}
	
	
}

Package com.xxjz.sendemail;

Import java.io.Serializable;
	public class MailBox implements Serializable {private static final long serialversionuid = -543077416549260588l;          Private String Mailsmtpauth;          SMTP server verifies private String mailsmtphost;          SMTP server private String Mailsmtpport;              Port number, QQ mailbox gives two ports private String mailuser;          Account Private String MailPassword;
	Password public String Getmailsmtpauth () {return mailsmtpauth;
	} public void Setmailsmtpauth (String mailsmtpauth) {this.mailsmtpauth = Mailsmtpauth;
	Public String Getmailsmtphost () {return mailsmtphost;
	} public void Setmailsmtphost (String mailsmtphost) {this.mailsmtphost = Mailsmtphost;
	Public String Getmailsmtpport () {return mailsmtpport;
	} public void Setmailsmtpport (String mailsmtpport) {this.mailsmtpport = Mailsmtpport;
	Public String Getmailuser () {return mailuser; } public void Setmailuser (String mailuser) {this.mailuser = Mailuser;
	Public String Getmailpassword () {return mailpassword;
	} public void Setmailpassword (String mailpassword) {This.mailpassword = MailPassword;
	public static long Getserialversionuid () {return serialversionuid;
 }
	
	
}


Before you send a message, you need to add Javax.mail this dependency pack. Because I use the MAVEN project, I go directly to the code and add in the Pom file:

<!--https://mvnrepository.com/artifact/javax.mail/mail-->
		<dependency>
		    <groupId> javax.mail</groupid>
		    <artifactId>mail</artifactId>
		    <version>1.4.7</version >
		</dependency>

The next step is to send the core code of the message:

Package com.xxjz.sendemail;

Import java.util.Properties;
Import Javax.mail.Authenticator;
Import javax.mail.MessagingException;
Import javax.mail.PasswordAuthentication;
Import javax.mail.Session;
Import Javax.mail.Transport;
Import javax.mail.internet.AddressException;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeMessage;



Import Javax.mail.internet.MimeMessage.RecipientType;
	public class SendMail {private static volatile SendMail singleton = null;
	Private MailBox MailBox = new MailBox ();

	Private Mail mail = new mail (); Private SendMail () {} public static SendMail Getsingleton () {if (singleton = = null) {synchronized (SENDMAIL.CLA
				SS) {if (singleton = = null) {singleton = new SendMail ();
	}} return singleton;
	public void SetProperties (MailBox b) {this.mailbox = b;
	public void Setmail (Mail b) {this.mail = b; public void Sendqqsmtpemail () throws Exception {//Create properties class to record some attributes of a mailbox finAl Properties Props = new properties ();
		Indicates SMTP sends mail, must authenticate Props.put ("Mail.smtp.auth", Mailbox.getmailsmtpauth ());
		Here is the SMTP server Props.put ("Mail.smtp.host", Mailbox.getmailsmtphost ());
		Port number, QQ mailbox gives two port props.put ("Mail.smtp.port", Mailbox.getmailsmtpport ());
		Here is your account props.put ("Mail.user", Mailbox.getmailuser ());

		The password here is the previous stmp password props.put ("Mail.password", Mailbox.getmailpassword ()); Build authorization information for SMTP authentication Authenticator authenticator = new Authenticator () {protected passwordauthentication Getpas
				Swordauthentication () {//username, password String userName = Props.getproperty ("Mail.user");
				String Password = props.getproperty ("Mail.password");
			return new Passwordauthentication (userName, password);
		}
		};
		Create a mail conversation session mailsession = Session.getinstance (props, authenticator) using ambient properties and authorization information;
		Create a mail message mimemessage messages = new MimeMessage (mailsession); Set Sender internetaddress form = new InternetAddress (Props.getproperty ("mail.uSer "));

		Message.setfrom (form);
		Set the recipient's mailbox internetaddress to = new InternetAddress (Mail.getsendto ());

		Message.setrecipient (recipienttype.to, to);

		Set the message header Message.setsubject (Mail.getsubject ());

		Set the content body of the message message.setcontent (Mail.gettext (), "text/html;charset=utf-8");
	Finally send the message transport.send (messages);
 }

}

OK, now write a test class to verify the.


Package com.xxjz.sendemail;

 
public class TEST {public
	static void Main (string[] args) throws Exception {SendMail
		SendMail = sendmail.getsing Leton ();
		MailBox MailBox = new MailBox ();
		Mail mail = new mail ();
		Mailbox.setmailsmtpauth ("true");
		Mailbox.setmailsmtphost ("smtp.qq.com");
		Mailbox.setmailsmtpport ("587");
		Mailbox.setmailuser ("xxxxxxxxx@qq.com");
		Mailbox.setmailpassword ("Geewtndcrvawqebe");
		Mail.setsendto ("xxxxxxxxxx@qq.com");
		Mail.settext ("Test * Hello world\n");
		Mail.setsubject ("Testing Test");
		Sendmail.setproperties (MailBox);
		Sendmail.setmail (mail);
		Sendmail.sendqqsmtpemail ();
	}
First set the mailbox properties and the contents of the message, and then send ...

The effect diagram, as shown in the figure:




I wrote with spring boot, and interested partners can go down and look at the GitHub, and here's the address:

Https://github.com/zdxxinlang/SendEmailSpringBoot.git

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.