我可以发送邮件gmail
,godaddy mail service
但相同的代码在这里不起作用exchange server
。
它的抛出异常像
线程“主”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();
}
}
}