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 ();
}
}
|