如何使用microsoft exchange服务器使用javamail 1.5.1发送邮件?
我花了一天的时间搜索如何使用MicrosoftExchangeServer使用javamail 1.5.1发送邮件,但我没有找到适合我的项目的解决方案如何使用microsoft exchange服务器使用javamail 1.5.1发送邮件?,java,email,servlets,jakarta-mail,exchange-server,Java,Email,Servlets,Jakarta Mail,Exchange Server,我花了一天的时间搜索如何使用MicrosoftExchangeServer使用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 =
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:对不起,我的英语…看看这是否有效:
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();
}
}
import java.util.Properties;
导入java.util.concurrent.ExecutorService;
导入java.util.concurrent.Executors;
导入java.util.concurrent.AtomicInteger;
导入javax.activation.DataHandler;
导入javax.activation.DataSource;
导入javax.activation.FileDataSource;
导入javax.mail.BodyPart;
导入javax.mail.Message;
导入javax.mail.Multipart;
导入javax.mail.Session;
导入javax.mail.Transport;
导入javax.mail.internet.MimeBodyPart;
导入javax.mail.internet.mimessage;
导入javax.mail.internet.MimeMultipart;
公共类EmailSenderWo附件{
专用静态最终整数MSGCOUNT=10;
静态AtomicInteger c=新的AtomicInteger();
静态会话;
静态类发送器实现可运行{
@凌驾
公开募捐{
虽然(c.get()您的选项talib2608中没有身份验证:(但是我查看了这些链接,现在我的身份验证工作正常了。(谢谢比尔·香农)
但是当我尝试时,我没有收到我的消息
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
当我在本地计算机上测试时,而不是在服务器上测试时,会出现此错误消息。您没有提供任何有关它如何工作的信息,因此很难猜出发生了什么问题。可能您没有提供正确的用户名或密码?请先清理这些用户名或密码,然后发布。
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