当前位置: 技术问答>java相关
那位大虾给个javaMAIL的例子???(内空)
来源: 互联网 发布时间:2015-03-25
本文导语: | import java.util.*; import java.io.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; // Copyright (c) 2000 OOCL public class MsgMultiSendSample extends Object { static String msgText1="hello !"; /** * Constructor...
|
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
// Copyright (c) 2000 OOCL
public class MsgMultiSendSample extends Object {
static String msgText1="hello !";
/**
* Constructor
*/
public MsgMultiSendSample() {
}
/**
* main
* @param args
*/
public static void main(String[] args) {
//MsgMultiSendSample msgMultiSendSample = new MsgMultiSendSample();
String to="Ming.Yang@sigma-elevator.com";
String from="Ming.Yang@sigma-elevator.com";
String host="202.101.25.211";
System.out.println("Pengda>>>>>>>>to, from and host set");
Properties props=new Properties();
props.put("mail.smtp.host",host);
System.out.println("Pengda>>>>>>>>>>>>properties set");
Session session = Session.getDefaultInstance(props,null);
try{
MimeMessage msg=new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address={new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO,address);
msg.setSubject("Hello");
msg.setSentDate(new Date());
MimeBodyPart mbp1=new MimeBodyPart();
mbp1.setText(msgText1);
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
msg.setContent(mp);
System.out.println("Pengda>>>>>>>>>>>about to send message....");
Transport.send(msg);
System.out.println("Pengda>>>>>>>>>>>Message sent");
}catch(MessagingException mex){
mex.printStackTrace();
Exception ex = null;
if((ex=mex.getNextException())!=null){
ex.printStackTrace();
}
}
}
}
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
// Copyright (c) 2000 OOCL
public class MsgMultiSendSample extends Object {
static String msgText1="hello !";
/**
* Constructor
*/
public MsgMultiSendSample() {
}
/**
* main
* @param args
*/
public static void main(String[] args) {
//MsgMultiSendSample msgMultiSendSample = new MsgMultiSendSample();
String to="Ming.Yang@sigma-elevator.com";
String from="Ming.Yang@sigma-elevator.com";
String host="202.101.25.211";
System.out.println("Pengda>>>>>>>>to, from and host set");
Properties props=new Properties();
props.put("mail.smtp.host",host);
System.out.println("Pengda>>>>>>>>>>>>properties set");
Session session = Session.getDefaultInstance(props,null);
try{
MimeMessage msg=new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address={new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO,address);
msg.setSubject("Hello");
msg.setSentDate(new Date());
MimeBodyPart mbp1=new MimeBodyPart();
mbp1.setText(msgText1);
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
msg.setContent(mp);
System.out.println("Pengda>>>>>>>>>>>about to send message....");
Transport.send(msg);
System.out.println("Pengda>>>>>>>>>>>Message sent");
}catch(MessagingException mex){
mex.printStackTrace();
Exception ex = null;
if((ex=mex.getNextException())!=null){
ex.printStackTrace();
}
}
}
}