Delve into the use of the JavaMail API

Source: Internet
Author: User
Tags mail

In my previous article "Use JavaMail and JSP combined to write a simple e-mail delivery system," introduced the JavaMail and JSP in combination with the method. In fact, the JavaMail API is a fairly large system, only then a small text is not able to introduce the end of JavaMail, so I would like to take this article with you in-depth discussion about the use of JavaMail.

Question one, how can I send a simple letter?

Answer: I will explain in detail how to send a simple letter in the following example

import java.util.*;
import java.io.*;
import javax.mail.*;
Import javax.mail.internet.*;
Import javax.activation.*;
public class Sendsimplemail {
File://msgText is the body of the letter, there are two lines
static String Msgtext = "Dear mr.fangzhou\ni ' m a read Er of your net! "
file://reads three arguments from the command line, smtphost,from,to
public static void Main (String args[])
throws exception{
if ( Args.length!= 3) {
System.out.println ("Usage:java sendsimplemail");
Return
}
String SMTPHOST=ARGS[0];//SMTP server name
String from=args[1];//Sender address
String to =args[2];//delivery address
// Create Properties Object
Properties props = new properties ();
file://Create mail server
Props.put ("Mail.smtp.host", smtphost);
FILE://gets the default session
Session session = Session.getdefaultinstance (props, null);
//Create a message and define the sender's address and delivery address
MimeMessage message = new MimeMessage (session);
Message.setfrom (A new internetaddress (from));
Internetaddress[] address = {new internetaddress (to)};
MessagE.setrecipients (Message.RecipientType.TO, address);
Message.setsubject ("Hello,fangzhou");/Set Theme
Message.setsentdate (new Date ());/Set Send time
Message.settext ( Msgtext)//Set the text in the previously defined msgtext to the content of the message body
file://send mail
transport.send (message);
}
}

Question two, what if I want to add an attachment to the email?

This is also a pretty good question, to add an attachment is to use MimeBodyPart to create the message, just the above procedures can be slightly modified, for the sake of simplicity, I just use a string as an attachment content to explain how to add an attachment.

File://msgText is the body of the letter, there are two lines

static String msgText = "Dear Mr.FangZhou\nI’m a reader of your net!"
file://msgAttachment是一段字符串作为附件
static String msgAttachment = "This is an attachment string!"
. . . . . . . . .
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(msgText); file://把前面定义的msgText中的文字设定为邮件正 文的内容
file://创建附件部分
MimeBodyPart mbp2 = new MimeBodyPart();
file://使用setText(text, charset)来加入附件
mbp2.setText(msgAttachment, "gb2312");
file://创建Multipart
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
// 添加 Multipart到Message中
message.setContent(mp);
file://发送邮件
Transport.send(message);
}

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.