日期:2014-05-20  浏览次数:20854 次

java中自带的邮件接收发送类,那个最好??
如题,最好能把jar给附上

------解决方案--------------------
JavaMail Sender
/*
 * MailSend.java
 *
 * Created on March 17, 2008, 2:16 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package javamail;

import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author winxp
 */
public class MailSend extends Object{

/** Creates a new instance of MailSend */
public MailSend(){
}

public static void main(String []args) {
String smtpServer = null;
String toEmail = null;
String fromEmail = null;
String body = null;
for(int x=0;x<args.length-1;x++){
if(args[x].equalsIgnoreCase("-S"))
smtpServer = args[x+1];
else if(args[x].equalsIgnoreCase("-T"))
toEmail = args[x+1];
if(args[x].equalsIgnoreCase("-F"))
fromEmail = args[x+1];
if(args[x].equalsIgnoreCase("-B"))
body = args[x+1];
}
if(smtpServer== null || toEmail==null|| fromEmail==null ||body==null){
System.out.println("Usage:javamail_send -S <server> -T <toEmail> -F <fromEmail> -B<body>");
System.exit(1);
}
send(smtpServer,toEmail,fromEmail,body);
}

public static void send(String smtpServer,String toEmail,String fromEmail,String body) {

try{
Properties props = new Properties();
props.put("mail.transport.protocol","smtp");
props.put("mail.smtp.host",smtpServer);
props.put("mail.smtp.port","25");
props.put("mail.smtp.auth","true");

Session mailSession = Session.getDefaultInstance(props,new JMTomAuthenticator());
Message msg= new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(fromEmail));
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(toEmail));
msg.setSentDate(new Date());
msg.setSubject("Test Mail");
msg.setText(body);
//Transport tra = new Transport();
//tra.connect("")
Transport.send(msg);
System.out.println("The email below was sent successfully");
msg.writeTo(System.out);
}catch(Exception ex){
System.out.println(ex);
}
}

}