Java mail test

mail局域网Exchange服务器测试代码,仅限发送到domain内用户,发送外网需要exchange设置白名单, ref:
解决方案:
Exchange 2003:
Open the Exchange System Manager
Go in Administrative Groups -> Administrative group name -> Server -> Server name -> Protocols -> SMTP
Right click on Default SMTP Virtual Server -> properties
Access tab -> Relay button
Select "only the list below" and add the domain and\or IPs of your JIRA server
Exchange 2007:
For Exchange 2007, create a second receive connector for external addresses as described in 2007[url=https://confluence.atlassian.com/display/JIRA/How+to+Set+Up+SMTP+Relay+in+Exchange+2007]How to Set Up SMTP Relay in Exchange [/url].
(info) Check Microsoft's [url=http://msexchangeteam.com/archive/2006/12/28/432013.aspx]Exchange Archive[/url] as well.
Office 365:
Check [url=http://support.microsoft.com/kb/2572646]Microsoft's KB[/url] article on this.

package com.test.mail;

import java.util.Date;
import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class MailTest {
    private String host;

    private String auth;

    private String username;

    private String domainUser;

    private String password;

    public MailTest(String host, String auth, String domainUser,
            String username, String password) {
        super();
        this.host = host;
        this.auth = auth;
        this.domainUser = domainUser;
        this.username = username;
        this.password = password;
    }

    public boolean send(String[] to, String[] cc, String[] bcc, String subject,
            String content) throws MessagingException {
        Properties props = new Properties();
        props.put("mail.smtp.host", this.host);
        props.put("mail.smtp.auth", this.auth);
        Session s = Session.getInstance(props);
        s.setDebug(true);

        MimeMessage message = new MimeMessage(s);

        InternetAddress from = new InternetAddress(this.username);
        message.setFrom(from);
        InternetAddress[] Toaddress = new InternetAddress[to.length];
        for (int i = 0; i < to.length; i++) {
            Toaddress[i] = new InternetAddress(to[i]);
        }
        message.setRecipients(Message.RecipientType.TO, Toaddress);

        if (cc != null) {
            InternetAddress[] Ccaddress = new InternetAddress[cc.length];
            for (int i = 0; i < cc.length; i++) {
                Ccaddress[i] = new InternetAddress(cc[i]);
            }
            message.setRecipients(Message.RecipientType.CC, Ccaddress);
        }

        if (bcc != null) {
            InternetAddress[] Bccaddress = new InternetAddress[bcc.length];
            for (int i = 0; i < bcc.length; i++) {
                Bccaddress[i] = new InternetAddress(bcc[i]);
            }
            message.setRecipients(Message.RecipientType.BCC, Bccaddress);
        }
        message.setSubject(subject);
        message.setSentDate(new Date());

        BodyPart mdp = new MimeBodyPart();
        mdp.setContent(content, "text/html;charset=utf-8");
        Multipart mm = new MimeMultipart();
        mm.addBodyPart(mdp);
        message.setContent(mm);

        message.saveChanges();
        Transport transport = s.getTransport("smtp");
        transport.connect(this.host, (null == this.domainUser) ? this.username
                : this.domainUser, this.password);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
        return true;
    }

    public static void main(String[] args) {
        try {
            MailTest mailTest = new MailTest("edgeexc.xxxx.com", "true",
                    "SSICN\\vince.carter", "[email protected]",
                    "pwdtest");
            mailTest.send(
                    new String[] { "[email protected]" },
                    null,
                    null,
                    "Test smtp server",
                    "<h3>Test exchange server sending mail to normal mail servers like: domain, qq, gmail, 163.</h3>");
        }
        catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}


未加入白名单IP发送时,外网邮箱会提示:
DEBUG SMTP: Valid Unsent Addresses
DEBUG SMTP:   [email protected]
DEBUG SMTP:   [email protected]
DEBUG SMTP: Invalid Addresses
DEBUG SMTP:   [email protected]
DEBUG SMTP: Sending failed because of invalid destination addresses
RSET
250 2.0.0 Resetting
javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
	com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay;

你可能感兴趣的:(java,domain,mail,Exchange,javamail)