回答: 发送HTML格式的邮件voID jButton8_actionPerFormed(ActionEvent e) {
try
{//此段代码用来发送带有HTML信息的邮件
Properties PRoPS = new Properties();
Authenticator Auth = new EmAIl_Autherticator();
PRoPS.put( "mAIl.smtp.host ",host);
PRoPS.put( "mAIl.smtp.Auth ", "true ");
Session session = Session.getDefAultInstance(PRoPS,Auth);
MimeMessage message = new MimeMessage(session);
//设置邮件正文
String HTMLtext= "
hello
"+ " click me please! ";message.setContent(HTMLtext, "text/HTML ");//text/plAIn设置邮件的格式因为是带有HTML文档的,所以用text/HTML格式
message.setSubject(mAIl_SUbject);
// message.setText(mAIl_body);在使用message.setContent()时候,好象不能用message.setText()
message.setHeader(mAIl_head_name,mAIl_head_value);
message.setSentDate(new Date());
Address address = new InternetAddress(mAIl_from, "SUnxiaoming ");
message.setFrom(address);
Address toAddress = new InternetAddress(mAIl_to);
message.addRecipient(Message.RecipientType.TO,toAddress);
Transport.send(message);
System.out.PRintln( "send ok! ");
}
catch(Exception ex)
{
System.out.PRintln( "fAIld "+ex);
}
}
voID jButton9_actionPerFormed(ActionEvent e) {
try
{//此段代码用来发送带有图片的邮件
String file = "./20020423/test.gif ";
Properties PRoPS = new Properties();
Authenticator Auth = new EmAIl_Autherticator();
PRoPS.put( "mAIl.smtp.host ",host);
PRoPS.put( "mAIl.smtp.Auth ", "true ");
Session session = Session.getDefAultInstance(PRoPS,Auth);
MimeMessage message = new MimeMessage(session);
message.setSubject( "Embedded Image ");
Address address = new InternetAddress(mAIl_from, "SUnxiaoming ");
message.setFrom(address);
Address toAddress = new InternetAddress(mAIl_to);
message.addRecipient(Message.RecipientType.TO,toAddress);
message.setHeader(mAIl_head_name,mAIl_head_value);
message.setSentDate(new Date());
BodyPart messageBodyPart = new MimeBodyPart();
String HTMLText = "
"+ " ";//加入图片
messageBodyPart.setContent(HTMLText, "text/HTML ");
//原理同发送附件一样,将邮件看成有多个部分组成的,先设置每一个部分,然后再全部将它们合起来即可。
//Create a related multi-part to combine the parts
MimeMultipart multipart = new MimeMultipart( "related ");
multipart.addBodyPart(messageBodyPart);
//Create part For the image
messageBodyPart = new MimeBodyPart();
//Fetch the image and associate to part
DataSource fds = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader( "Content-ID ", "memememe ");
//Add part to multi-part
multipart.addBodyPart(messageBodyPart);
//Associate multi-part with message
message.setContent(multipart);
Transport.send(message);
System.out.PRintln( "send ok! ");
}
catch(Exception ex)
{
System.out.PRintln( "fAIld "+ex);
}
}
voID jButton7_actionPerFormed(ActionEvent e) {
try
{//该程序用来接受带有HTML信息的邮件
Properties PRoPS = System.getProperties();
Authenticator Auth = new EmAIl_Autherticator();
PRoPS.put( "mAIl.smtp.host ",host);
PRoPS.put( "mAIl.smtp.Auth ", "true ");
Session session = Session.getDefAultInstance(PRoPS,Auth);
Store store = session.getStore( "pop3 ");
store.connect(host,username,pasSWord);
//After connecting to the Store,you can get a Folder,which must be opened beFore you can read messages from it:
Folder folder = store.getFolder( "INBOX ");
folder.open(Folder.READ_WRITE);
Message message[] = folder.getMessages();
For (int i=0,n=message.length;i
// For (int i=0,n=1;i
{
String out_from_person = ((InternetAddress)message[i].getFrom()[0]).getPersonal();
String out_from_address = ((InternetAddress)message[i].getFrom()[0]).getAddress();
System.out.PRintln( "From: "+out_from_person+ "\t ");
System.out.PRintln( "Address: "+out_from_address+ "\t ");
String out_SUbject = message[i].getSubject();
System.out.PRintln( "Subject: "+out_SUbject+ "\t ");
//获取邮件的信息,并且对其每一部分进行操作
Part messagePart = message[i];
Object out_content = messagePart.getContent();
if (out_content instanceof Multipart)
{
messagePart = ((Multipart)out_content).getBodyPart(0);
System.out.PRintln( "[ Multipart Message ] ");
}
String out_content_type = messagePart.getContentType();
System.out.PRintln( "CONTENT: "+out_content_type);
jEditorPane1.setText( " ");//将其显示在jEditorPanel上
jEditorPane1.setContentType( "text/HTML ");
jEditorPane1.setText((String)out_content);
jEditorPane1.setEditable(false);
System.out.PRintln( "------------------------------------------------------------ ");
message[i].setFlag(Flags.Flag.DELETED,true);
}
folder.close(true);
store.close();
}
catch(Exception ej2)
{
System.out.PRintln(ej2);
}
}
如果觉得《邮件内容以html形式 如何以邮件的形式发送HTML文件?》对你有帮助,请点赞、收藏,并留下你的观点哦!