Java Mail send and receive (only English, Chinese required transcoding)

Source: Internet
Author: User

Outgoing items

Package com.sun.mail;

Import java.io.UnsupportedEncodingException;
Import java.util.Properties;

Import Javax.mail.Authenticator;
Import Javax.mail.Message;
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;

public class SendMail2 {
public static void Main (string[] args) throws Unsupportedencodingexception {
Boolean Isssl = true;
String host = "smtp.163.com";
int port = 465;
String from = "Sender address";
String to = "Recipient address";
Boolean Isauth = true;
Final String username = "Sender address";
Final String Password = "Sender address password";

Properties Props = new properties ();
Props.put ("mail.smtp.ssl.enable", Isssl);
Props.put ("Mail.smtp.host", host);
Props.put ("Mail.smtp.port", port);
Props.put ("Mail.smtp.auth", Isauth);

Session session = Session.getdefaultinstance (props, new Authenticator () {
@Override
Protected Passwordauthentication getpasswordauthentication () {
return new passwordauthentication (username, password);
}
});

try {
Message message = new MimeMessage (session);

Message.setfrom (New InternetAddress (from));
Message.addrecipient (Message.RecipientType.TO, New internetaddress (to));
Message.setsubject ("Test Mail");
Message.settext ("bingo!");

Transport.send (message);
} catch (Addressexception e) {
E.printstacktrace ();
} catch (Messagingexception e) {
E.printstacktrace ();
}

SYSTEM.OUT.PRINTLN ("Send complete! ");
}
}

Receive items

Package com.sun.mail;

Import java.io.IOException;
Import Java.util.Date;
Import java.util.Properties;
Import Javax.mail.Folder;
Import Javax.mail.Message;
Import javax.mail.MessagingException;
Import javax.mail.NoSuchProviderException;
Import javax.mail.Session;
Import Javax.mail.Store;

public class FetchMail2 {
public static void Main (string[] args) throws IOException {
String protocol = "POP3";
Boolean Isssl = true;
String host = "pop.qq.com";
int port = 995;
String username = "Recipient Address";
String Password = "recipient address password";//qq now requires an authorization code

Properties Props = new properties ();
Props.put ("mail.pop3.ssl.enable", Isssl);
Props.put ("Mail.pop3.host", host);
Props.put ("Mail.pop3.port", port);

Session session = Session.getdefaultinstance (props);

Store store = null;
Folder folder = null;
try {
store = session.getstore (protocol);
Store.connect (username, password);

folder = Store.getfolder ("INBOX");
Folder.open (folder.read_only);

int size = Folder.getmessagecount ();
Message message = folder.getmessage (size);

String from = Message.getfrom () [0].tostring ();
String subject = Message.getsubject ();
Date date = Message.getsentdate ();

System.out.println ("From:" + from);
System.out.println ("Subject:" + Subject);
System.out.println ("Content:" +message.getcontent ());
System.out.println ("Date:" + date);
} catch (Nosuchproviderexception e) {
E.printstacktrace ();
} catch (Messagingexception e) {
E.printstacktrace ();
} finally {
try {
if (folder! = null) {
Folder.close (FALSE);
}
if (store! = null) {
Store.close ();
}
} catch (Messagingexception e) {
E.printstacktrace ();
}
}

SYSTEM.OUT.PRINTLN ("Reception complete! ");
}
}

Java Mail send and receive (only English, Chinese required transcoding)

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.