请帮我修一下这个错误弹出窗口说:
javax.mail.NoSuchProviderException:没有smtp的提供者
我添加了导入:
import java.io.File;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
如果我能得到一些帮助,我将非常感激。
private void SendButtonActionPerformed(java.awt.event.ActionEvent evt) {
String To = to.getText();
String Subject = subject.getText();
String Text_content = jTextArea1.getText();
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("user@gmail.com", "user123");
}
}
);
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("user@gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(To));
message.setSubject(Subject);
message.setText(Text_content);
Transport.send(message);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
答案 0 :(得分:0)
使用您的凭据尝试使用以下代码。如果您在代理后面,请在setProxy
方法中添加代理配置。
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class TestMail {
public static void main(String[] args) {
sendMail();
}
private static void sendMail() {
setProxy();
System.out.println("Started");
String Subject = "Test mail";
String Text_content = "";
String mailId = "user@gmail.com";
String pass = "userpass";
String To = mailId;
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", true); // added this line
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.user", mailId);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", true);
Session session = Session.getDefaultInstance(props, null);
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress());
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(To));
message.setSubject(Subject);
message.setText(Text_content);
Transport transport = session.getTransport("smtp");
transport.connect(props.getProperty("mail.smtp.host"), mailId, pass);
System.out.println("Transport: " + transport.toString());
transport.sendMessage(message, message.getAllRecipients());
} catch (Exception e) {
e.printStackTrace();
}
}
private static void setProxy() {
/*System.setProperty("http.proxyHost", "");
System.setProperty("http.proxyPort", "");
System.setProperty("https.proxyHost", "");
System.setProperty("https.proxyPort", "");
*/
}
}