我已经过了一天的时间来搜索如何使用微软交换服务器用javamail 1.5.1发送邮件,而我没有为我的项目找到解决方案。
public static void sendMail(String message_dest,String message_objet,String message_corps){
Authenticator auth;
Session session;
Message mesg;
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.port", "25");
props.put("mail.smtp.host", "10.X.X.X");
props.put("mail.smtp.auth", "true");
//Authenticator auth = new MyAuthentificator();
auth = new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
"xx@xx.com", "xx");
}
};
session = Session.getDefaultInstance(props, auth);
session.setDebug(true);
try {
mesg = new MimeMessage(session);
mesg.setFrom(new InternetAddress(xx@xx.com));
InternetAddress toAddress = new InternetAddress(message_dest);
mesg.addRecipient(Message.RecipientType.TO, toAddress);
mesg.setSubject(message_objet);
mesg.setText(message_corps);
Transport.send(mesg);
} catch (MessagingException ex) {
while ((ex = (MessagingException)ex.getNextException()) != null) {
ex.printStackTrace();
}
}
}
我已经尝试在其他课程中移除我的身份验证,但它也不会起作用......
请帮助:(
PS:对不起我的英文......
答案 0 :(得分:0)
看看是否有效:
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class EmailSenderWWOAttachment {
private static final int MSGCOUNT = 10;
static AtomicInteger c = new AtomicInteger();
static Session session;
static class Sender implements Runnable {
@Override
public void run() {
while(c.get()<MSGCOUNT){
int i = c.incrementAndGet();
try {
// email without attachment
MimeMessage msg1 = new MimeMessage(session);
msg1.setFrom("mailbox1@abc.com");
msg1.setRecipients(Message.RecipientType.TO, "mailbox2@abc.com");
msg1.setSubject("Subject of email: " + i);
msg1.setText("Body of email: " + i);
Transport.send(msg1);
System.out.println("Email no. " + i + " without attachment sent");
// email with attachment
MimeMessage msg = new MimeMessage(session);
msg.setFrom("mailbox3@abc.com");
msg.setRecipients(Message.RecipientType.TO, "mailbox4@abc.com");
msg.setSubject("Subject of email: " + i);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Body of email: " + i);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
BodyPart attachBodyPart = new MimeBodyPart();
String filename = "C:\\Users\\user1\\Desktop\\1.txt";
DataSource source = new FileDataSource(filename);
attachBodyPart.setDataHandler(new DataHandler(source));
attachBodyPart.setFileName("1.txt");
multipart.addBodyPart(attachBodyPart);
msg.setContent(multipart);
Transport.send(msg);
System.out.println("Email no. " + i + " with attachment sent");
} catch (Exception e) {
e.printStackTrace();
c.decrementAndGet();
}
}
}
}
public static void main( String[] args ) {
Properties props = new Properties();
props.put( "mail.smtp.host", "exchange-server" );
session = Session.getInstance( props, null );
ExecutorService executorService = Executors.newFixedThreadPool( 20 );
for( int i=0; i<20; i++ ) {
executorService.execute( new Sender() );
}
executorService.shutdown();
}
}
答案 1 :(得分:0)
您的选项talib2608中没有身份验证:(但我查看了这些链接,现在我的身份验证工作正常。(感谢Bill shannon)
http://www.oracle.com/technetwork/java/javamail/faq/index.html#commonmistakes http://www.oracle.com/technetwork/java/javamail/faq/index.html#debug
public static void sendMail(String message_dest,String message_objet,String message_corps){
Authenticator auth;
Session session;
Message mesg;
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.port", SMTP_PORT1);
props.put("mail.smtp.host", SMTP_HOST1);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.ssl.enable", "true");
session = Session.getInstance(props);
session.setDebug(true);
try {
mesg = new MimeMessage(session);
mesg.setFrom(new InternetAddress(IMAP_ACCOUNT1));
InternetAddress toAddress = new InternetAddress(message_dest);
mesg.addRecipient(Message.RecipientType.TO, toAddress);
mesg.setSubject(message_objet);
mesg.setText(message_corps);
Transport t = session.getTransport("smtp");
try {
t.connect("xx@xx.com", "mypwd");
t.sendMessage(mesg, mesg.getAllRecipients());
} finally {
t.close();
}
}catch (MessagingException ex){
System.out.println(ex);
}
}
但是当我尝试它时,我没有收到我的信息......
DEBUG: setDebug: JavaMail version 1.5.2
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "xx.xx.xxx.xx", port 25, isSSL true
javax.mail.MessagingException: Could not connect to SMTP host: xx.xx.xxx.xx, port: 25;
nested exception is:
java.net.SocketException: Connection reset
当我在本地计算机上测试时出现此错误消息,而不是在服务器上。