我可以通过gmail
和godaddy mail service
发送邮件,但相同的代码在exchange server
处不起作用。
抛出异常,如
线程“main”中的异常javax.mail.MessagingException:不能 连接到SMTP主机:。。。,端口:**
请帮帮我。
这是我的代码:
public void sendMailsTest(String hostName, int portNo, final String userLoginId,
final String userPassword, String mailtoUser, String userName,
DocumentDetails mailDetails,String fromUserName, int type,
List<User> mailBodyDetails,ArrayList<String> ccList) {
String line1 = "This is an auto generated e-Mail don't reply";
String mailto=mailtoUser;
String from =userLoginId;
for(int i=0; i<mailBodyDetails.size();i++){
if(mailBodyDetails.get(i).getMailTypeId() == type){
subject = mailBodyDetails.get(i).getMailSubject();
body = "body";
}
}
boolean sessionDebug=false;
String host=hostName;
int portno=portNo;
String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
Properties props= new Properties(System.getProperties());
props.put("mail.host",host);
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.transport.protocol","smtp");
props.put("mail.smtp.port",""+portno);
props.put("mail.smtp.auth","true");
//props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
props.put("mail.smtp.socketFactory.fallback", "false");
Authenticator auth = new Authenticator()
{
public PasswordAuthentication getPasswordAuthentication()
{
String username = userLoginId;
String password = userPassword;
return new PasswordAuthentication(username, password);
}
};
Session mailSession=Session.getInstance(props, auth);
mailSession.setDebug(sessionDebug);
MimeMessage msg = new MimeMessage(mailSession);
try {
msg.setFrom(new InternetAddress(from));
//InternetAddress[] CCaddress = new InternetAddress[communicationList.size()];
InternetAddress[] CCaddress = new InternetAddress[ccList.size()];
if(ccList.size() != 0){
for(int i =0; i< ccList.size(); i++)
{
if(ccList != null){
CCaddress[i] = new InternetAddress(ccList.get(i));
}
}
}
if(mailto != null){
InternetAddress[] address={new InternetAddress(mailto)};
msg.setRecipients(Message.RecipientType.TO, address);
}
msg.setRecipients(Message.RecipientType.CC, CCaddress);
msg.setSubject(subject);
msg.setContent("text/html");
msg.saveChanges();
Transport.send(msg);
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
try {
throw new MyTechnicalException("Messaging Server Is down,try after sometime");
} catch (MyTechnicalException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
try {
throw new MyTechnicalException("Messaging Server Is down,try after sometime");
} catch (MyTechnicalException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
答案 0 :(得分:0)
每个邮件服务器都有自己的一套规则和身份验证机制。更好地检查您的管理员与交换服务器相关的SMTP详细信息,如端口号,是否启用ssl,是否需要身份验证,如果是,则与您的IMAP身份验证详细信息相同。