[英]Sending mail example using javax.mail
I downloaded the following example: 我下载了以下示例:
https://app.box.com/shared/etj0lts287r5lc1hqeex https://app.box.com/shared/etj0lts287r5lc1hqeex
When I executed the jar file directly, it worked perfectly. 当我直接执行jar文件时,它运行良好。 But while running the project using Netbeans 7.0, it is throwing an error:
但是在使用Netbeans 7.0运行项目时,它会引发错误:
error: could not connect to SMTP host:smtp.gmail.com,port:465
What could be the issue? 可能是什么问题?
this is my code on button actionperformed private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) { 这是我关于按钮动作执行的代码私有void sendButtonActionPerformed(java.awt.event.ActionEvent evt){
boolean isSent = true;
try {
Properties properties = new Properties();
properties.setProperty("mail.smtp.submitter", txtfrom.getText());
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.host", txthostName.getText());
properties.put("mail.smtp.user", txtfrom.getText());
properties.put("mail.smtp.port", txtPort.getText());
properties.put("mail.smtp.socketFactory.port", txtPort.getText());
properties.put("mail.smtp.starttls.enable","true");
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.socketFactory.fallback", "false");
Authenticator mailAuthenticator = new MailAuthenticator();
Session mailSession = Session.getDefaultInstance(properties,mailAuthenticator);
Message message = new MimeMessage(mailSession);
Transport transport = mailSession.getTransport("smtps");
transport.connect("smtp.gmail.com",465,"abc@gmail.com","12345");
InternetAddress fromAddress = new InternetAddress(txtfrom.getText());
InternetAddress toAddress = new InternetAddress(txtto.getText());
message.setFrom(fromAddress);
message.setRecipient(RecipientType.TO, toAddress);
message.setSubject(txtsubject.getText());
message.setText(txtcontent.getText());
Transport.send(message);
} catch (Exception e) {
lblInfo.setText("ERROR:" + e.getMessage());
isSent = false;
}
if(isSent == true) {
sendButton.setEnabled(false);
lblInfo.setText("Your e-mail has been sent.");
}
}
Please consider making following changes to your code, 请考虑对代码进行以下更改,
// used "smtps" :) //使用“ smtps” :)
transport = mailSession.getTransport("smtps");
// port 465 worked for authentication and it solved with gmail, yahoo hosts transport.connect("smtp.gmail.com",465,"myid","password");
//端口465用于身份验证,并通过gmail解决了,雅虎主机
transport.connect("smtp.gmail.com",465,"myid","password");
JavaMail is the traditional answer for sending email. JavaMail是发送电子邮件的传统答案。 Please note how the
port
and other configuration is set. 请注意如何设置
port
和其他配置。
Here is an example to do it. 这是一个示例。
package example.java.mail;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class SendFromGmail {
public static void main(String args[]){
try{
String host = "smtp.gmail.com";
String from = "example@gmail.com";
String pass = "mypassword123";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
String[] to = {"example@gmail.com"};
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for( int i=0; i < to.length; i++ ) { // changed from a while loop
toAddress[i] = new InternetAddress(to[i]);
}
System.out.println(Message.RecipientType.TO);
for( int i=0; i < toAddress.length; i++) { // changed from a while loop
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject("sending in a group");
message.setText("Welcome to JavaMail");
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
catch(Exception e){
e.getMessage();
}
}
}
Check out for more Java mail sending examples. 查看更多Java邮件发送示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.