我无法使用Java Mail API从Yahoo
ID发送电子邮件。我尝试了Google提供的其他选项,但失败了。请查看下面的代码,如果我缺少什么,请告诉我。以我的观点, 雅虎
不提供免费的发送邮件服务,但我不确定。请提供您对此的想法。
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
public class MailExample {
private static final String SMTP_HOST_NAME = "smtp.mail.yahoo.com";
private static final int SMTP_HOST_PORT = 587;//465,587,25
private static final String SMTP_AUTH_USER = "dummyrls@yahoo.com";
private static final String SMTP_AUTH_PWD = "my password";
public static void main(String[] args) throws Exception{
new MailExample().test();
}
public void test() throws Exception{
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");
// props.put("mail.smtps.quitwait", "false");
Session mailSession = Session.getDefaultInstance(props);
mailSession.setDebug(true);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject("Testing SMTP-SSL");
message.setContent("This is a test", "text/plain");
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("rlss@abc.com"));
transport.connect
(SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
}
}
上面的代码适用于Gmail,但对于Yahoo来说,却出现如下错误:
DEBUG: setDebug: JavaMail version 1.4.1 DEBUG: getProvider()
returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,
Sun Microsystems, Inc.,1.4.1] DEBUG SMTP: useEhlo true,
useAuth true
DEBUG SMTP: trying to connect to host "smtp.mail.yahoo.com", port 587,
isSSL false Exception in thread "main"
javax.mail.MessagingException: Could not connect to SMTP
host: smtp.mail.yahoo.com, port: 587; nested exception is:
java.net.ConnectException: Connection timed out: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1391)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
at javax.mail.Service.connect(Service.java:288)
at com.sample.mailexample.MailExample.test(MailExample.java:313)
at com.sample.mailexample.MailExample.main(MailExample.java:291) Caused by:
java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:233)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1359)
... 4 more
我该如何解决?
试试这个代码
public class SendMail {
String host, port, emailid,username, password;
Properties props = System.getProperties();
Session l_session = null;
public BSendMail() {
host = "smtp.mail.yahoo.com";
port = "587";
emailid = "a@yahoo.com";
username = "a";
password = "pwd";
emailSettings();
createSession();
sendMessage("a@yahoo.com", "rahul@gmail.com","Test","test Mail");
}
public void emailSettings() {
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "false");
props.put("mail.smtp.port", port);
// props.put("mail.smtp.socketFactory.port", port);
// props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
// props.put("mail.smtp.socketFactory.fallback", "false");
}
public void createSession() {
l_session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
l_session.setDebug(true); // Enable the debug mode
}
public boolean sendMessage(String emailFromUser, String toEmail, String subject, String msg) {
//System.out.println("Inside sendMessage 2 :: >> ");
try {
//System.out.println("Sending Message *********************************** ");
MimeMessage message = new MimeMessage(l_session);
emailid = emailFromUser;
//System.out.println("mail id in property ============= >>>>>>>>>>>>>> " + emailid);
//message.setFrom(new InternetAddress(emailid));
message.setFrom(new InternetAddress(this.emailid));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
message.addRecipient(Message.RecipientType.BCC, new InternetAddress(AppConstants.fromEmail));
message.setSubject(subject);
message.setContent(msg, "text/html");
//message.setText(msg);
Transport.send(message);
System.out.println("Message Sent");
} catch (MessagingException mex) {
mex.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}//end catch block
return true;
}
}
我有一个IMAP服务器主机名:xyz.com.我的SMTP主机名是:comcast.net. 现在,当我通过Outlook发送电子邮件时,它会自动将我的ID[发件人ID]显示为:myusername@xyz.com而不是myusername@comcast.net. 我想用JavaMail API实现同样的目标。 当我使用JavaMail API时,它将发送者显示为myusername@comca
问题内容: 我正在使用sendgrid发送电子邮件,并且使用以下代码可以正常工作,但没有附件。 但是我需要发送附件,因此我搜索了github源和Web文档API,由于某种原因,没有javadocs,但是有一个示例GitHub sendgrid, 所以我一直在尝试直到它起作用为止,我缩小了一些异常和响应代码,起初我是被禁止的未经授权,最好是响应202,表示有效且已排队(在此处检查),这是我的代码发送
问题内容: 如何使用curl命令行程序从gmail帐户发送电子邮件? 我尝试了以下方法: 使用file.txt作为电子邮件的内容,但是,当我运行此命令时,出现以下错误: 是否可以从仍由curl托管的个人服务器托管帐户发送电子邮件?这样会使身份验证过程更容易吗? 问题答案: curl –url 'smtps://smtp.gmail.com:465’ –ssl-reqd \ –mail-from '
问题内容: 我正在尝试使用Java发送电子邮件: 我收到错误消息: 此代码可以发送电子邮件吗? 问题答案: 户名+密码不再是推荐的解决方案。这是因为 我尝试了此操作,Gmail向该代码中用作用户名的电子邮件发送了一封电子邮件,其中说我们最近阻止了你的Google帐户的登录尝试,并将我定向到此支持页面:support.google.com/accounts/answer/6010255因此它看起来很
问题内容: 这有点令人困惑,所以请在这里忍受… 我想建立一个系统,使用户可以通过我的网站发送模板化的电子邮件,但实际上并没有使用我的服务器发送它- 它只是打开自己的本地邮件客户端,并准备发送电子邮件。该应用程序将使用预定义的变量填充电子邮件的正文,以节省用户必须自己键入的电子邮件。然后,如果该消息不完全适合其目的,则他们可以根据需要编辑该消息。 我有很多理由希望它通过用户的本地邮件客户端,因此让服
它显示以下错误: JasperException:处理JSP页面/assignment3/send_email.JSP第132 129行时发生异常:Message Message=new MimeMessage(session1);130:131://Set FROM:header标头字段。132:message.setFrom(新InternetAddress(from));133:134://