简体   繁体   English

如何使用微软交换服务器使用javamail 1.5.1发送邮件?

[英]How to sending mail with javamail 1.5.1 using a microsoft exchange server?

I've passed my day to search how sending mail with javamail 1.5.1 using microsoft exchange server and i didn't found a solution for my project. 我已经过去了一天,搜索如何使用微软交换服务器发送邮件与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();
     }
  }
}

I've already try to move out my authentification in an other class but it won't work too... 我已经尝试在其他课程中移除我的身份验证,但它也不会起作用......

Please help :( 请帮忙 :(

PS: Sorry for my english... PS:抱歉我的英文...

See if this works: 看看这是否有效:

    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();
    }
}

There is no authentification in your option talib2608 :( But I looked at these links and now my authentication works fine. (thanks bill shannon) 您的选项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 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);
  }
}

But when I try it, i don't receive my message... 但是当我尝试它时,我没有收到我的信息......

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

This error message comes when i test on my local machine, not on the server. 当我在本地计算机上而不是在服务器上进行测试时,会出现此错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM