Java mail, mail, and mail attachment Implementation Program

Source: Internet
Author: User
Tags sendfile

This article describes how to use java mail to send mails, receive mails, and send mail attachments.

Send Email:

Java code

The Code is as follows: Copy code
Public class JavaMailTest {

Public static void main (String args []) throws MessagingException {
String smtpHost = "smtp.sohu.com ";
String from = "javamailfrom@sohu.com ";
String to = "javamailto@sohu.com ";

Properties properties = System. getProperties ();
Session session = Session. getInstance (properties );

MimeMessage message = new MimeMessage (session );
Message. setFrom (new InternetAddress (from ));
Message. addRecipient (Message. RecipientType. TO, new InternetAddress ());
Message. setSubject ("JavaMail Example ");
Message. setText ("Did it work? ");
Transport transport = session. getTransport ("smtp ");
Transport. connect (smtpHost ,"","");

Transport. sendMessage (message, message. getAllRecipients ());
Transport. close ();
}
}

The following error may occur:

'1970 5.1.1 <XXXX@ygsoft.com>: Recipient address rejected: User unknown in virtual mailbox table ',

The account you sent does not exist in the email system. Check whether your email address is entered incorrectly.

'554 5.7.1 <XXXX@ygsoft.com>: Sender address rejected: Access denied ',

Your email is an internal account, and the address you sent is not in the authorization domain

 

'1970 5.7.1 <XXXX@ygsoft.com>: Sender address rejected: not logged in ',

SMTP authentication is required for sending emails. The SMTP authentication part of your account is not set correctly. Please check the configuration

'1970 4.7.1 <unknown [***. ***]>: Client host rejected: Access denied'

Your IP address is blocked by the Administrator. Please check whether your machine is infected with viruses and send emails automatically
 

Receive email:

Java code

The Code is as follows: Copy code
Public class JavaMailReadMailTest {

Public static void main (String args []) throws MessagingException,
IOException {
String pop3Host = "pop3.sohu.com ";
String user = "******* @ sohu.com ";
String pass = "******";
Properties properties = System. getProperties ();
Session session = Session. getInstance (properties );
Store store = session. getStore ("pop3 ");
Store. connect (pop3Host, user, pass );

Folder folder = store. getFolder ("INBOX ");
Folder. open (Folder. READ_ONLY );
Message [] messages = folder. getMessages ();

For (int I = 0; I <messages. length; I ++ ){
System. out. println ("nFrom:" + messages [I]. getFrom () [0] + "n"
+ "Subject:" + messages [I]. getSubject ());
ContentType ct = new ContentType (messages [I]. getContentType ());
If ("text/html". equalsIgnoreCase (ct. getBaseType ())){
BufferedReader reader = new BufferedReader (
New InputStreamReader (messages [I]. getInputStream ()));
String s;
While (s = reader. readLine ())! = Null ){
System. out. println (s );
}
} Else {
Object o = messages [I]. getContent ();
If (o instanceof String ){
System. out. println (o );
} Else {
System. out. println (messages [I]. getContentType ());
If (o instanceof MimeMultipart ){
MimeMultipart mp = (MimeMultipart) o;
For (int j = 0; j <mp. getCount (); ++ j ){
MimeBodyPart bp = (MimeBodyPart) mp. getBodyPart (j );
System. out. println (bp. getContentType ());
}
}
}
}

}
}
}

Send attachments:

Java code

The Code is as follows: Copy code
Public class SendFile {
Public static void main (String args []) throws Exception {
String host = "smtp.sohu.com ";
String from = "*******";
String to = "*******";
String fileAttachment = "d: \ FileName ";

Properties properties = System. getProperties ();

Session session = Session. getInstance (properties, null );

MimeMessage message = new MimeMessage (session );
Message. setFrom (new InternetAddress (from ));
Message. addRecipient (Message. RecipientType. TO, new InternetAddress ());
Message. setSubject ("SendFile ");

MimeBodyPart messageBodyPart = new MimeBodyPart ();

MessageBodyPart. setText ("Hi ");

Multipart multipart = new MimeMultipart ();
Multipart. addBodyPart (messageBodyPart );

MessageBodyPart = new MimeBodyPart ();
DataSource source = new FileDataSource (fileAttachment );
MessageBodyPart. setDataHandler (new DataHandler (source ));
MessageBodyPart. setFileName (fileAttachment );
Multipart. addBodyPart (messageBodyPart );

Message. setContent (multipart );

Transport transport = session. getTransport ("smtp ");
Transport. connect (host ,"","");

Transport. sendMessage (message, message. getAllRecipients ());

Transport. close ();
}
}

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.