以下代码用于使用javamail api从java应用程序发送电子邮件(使用gmail)。
它在Linux(Kubuntu 16.04)上运行良好,但在Windows上没有开启Avast防病毒软件。
public void doSendGmail(){
Connection con = Functions.ConnectToDB();
try {
Statement stmt = con.createStatement();
String sqlQuery = "select * from settings";
ResultSet rs = stmt.executeQuery(sqlQuery);
while(rs.next()){
String email = rs.getString("Email");
//String pass = rs.getString("Password");
byte [] pass = rs.getBytes("Password");
cipher = Cipher.getInstance("DES/CTR/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, keySpec,ivspec);
byte [] plain_text = cipher.doFinal(pass);
from = email;
password = new String(plain_text);
}
con.close();// close the connection
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Error retrieving email address and password\n"+e.toString(),
"Error",JOptionPane.ERROR_MESSAGE);
}
to = txtTo.getText();
cc = txtCC.getText();
bcc = txtBCC.getText();
subject = txtSubject.getText();
message_body = jtaMessage.getText();
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
/*use authenticator as username and password are supplied 'on demand' i.e queried from database
or supplied via a login dialog*/
Session session = Session.getInstance(props,new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(from, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
if(!cc.equals("")){
message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc));
}
if(!bcc.equals("")){
message.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc));
}
message.setSubject(subject);
if(filePathList.isEmpty()){// if a file(s) have not been attached...
message.setText(message_body);
Transport.send(message);
}
else{// if a file(s) have been attached
MimeBodyPart textPart = new MimeBodyPart();
textPart.setText(message_body);// actual message
Multipart multipart = new MimeMultipart();// create multipart message
multipart.addBodyPart(textPart);//add the text message to the multipart
for(int i =0; i<filePathList.size(); i++){// use for loop to attach file(s)
MimeBodyPart attachmentPart = new MimeBodyPart();
DataSource source = new FileDataSource((String)filePathList.get(i));
attachmentPart.setDataHandler(new DataHandler(source));
attachmentPart.setFileName((String)fileList.get(i));
multipart.addBodyPart(attachmentPart);// add the attachment to the multipart
message.setContent(multipart);// add the multipart to the message
}
Transport.send(message);
}
JOptionPane.showMessageDialog(this, "Message Sent!","Sent",JOptionPane.INFORMATION_MESSAGE);
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Error sending email message\n"+e.toString(),
"Error",JOptionPane.ERROR_MESSAGE);
}
}
显示以下错误消息
当我禁用所有avast防护时,它会完美地发送邮件消息。
如何解决问题?
其他防病毒也有同样的效果吗?
答案 0 :(得分:0)
Avast介入与服务器的安全通信, 可能是因为它可以阻止您发送病毒或垃圾邮件。但既然如此 作为“中间人”,例外正确地告诉你 你的连接不安全。
JavaMail FAQ告诉您如何使用。来信任Avast证书
InstallCert程序将其添加到您的信任存储区,或者如何不检查
通过设置mail.smtp.ssl.trust
属性来完成证书。
答案 1 :(得分:0)
这就是我做的。我从此链接https://javaee.github.io/javamail/InstallCert.java下载了InstallCert程序。然后我使用文本编辑器(记事本++)打开文件,并将端口从443更改为465(gmail的SSL端口)并保存更改。之后我编译了文件并运行了提供gmail主机的程序,即java InstallCert smtp.gmail.com。生成了一个文件(jssecacerts)。我将此文件复制到$ JAVA_HOME / jre / lib / security文件夹中。我终于能够发送所有avast盾牌的电子邮件了!