java mail ssl smtp_无法使用Javamail通过SSL或TLS使用SMTP发送邮件

新年快乐!

我正在开发一个应用程序,用户只要发生特定触发器就会收到电子邮件.

这是我用来发送电子邮件的功能:

public static void sendEmail(String host, String port, String useSSL, String useTLS, String useAuth, String user, String password, String subject, String content, String type, String recipients)

throws NoSuchProviderException, AddressException, MessagingException {

final Properties props = new Properties();

props.setProperty("mail.transport.protocol", "smtp");

props.setProperty("mail.smtp.host", host);

props.setProperty("mail.smtp.port", port);

if (useSSL != null && !useSSL.equals("false") && useSSL.equals("true")) {

props.setProperty("mail.smtp.ssl.enable", useSSL);

props.setProperty("mail.smtp.socketFactory.class",

"javax.net.ssl.SSLSocketFactory");

props.setProperty("mail.smtp.socketFactory.port", port);

}

if (useTLS != null && !useTLS.equals("false") && useTLS.equals("true")) {

props.setProperty("mail.smtp.starttls.enable", useTLS);

props.setProperty("mail.smtp.socketFactory.fallback", "true");

}

props.setProperty("mail.smtp.auth", useAuth);

props.setProperty("mail.from", user);

props.setProperty("mail.smtp.user", user);

props.setProperty("mail.password", password);

Session mailSession = Session.getDefaultInstance(props, new Authenticator() {

protected PasswordAuthentication getPasswordAuthentication() {

return new PasswordAuthentication(props.getProperty("mail.smtp.user"), props

.getProperty("mail.password"));

}

});

Transport transport = mailSession.getTransport();

MimeMessage message = new MimeMessage(mailSession);

message.setHeader("Subject", subject);

message.setContent(content, type);

StringTokenizer tokenizer = new StringTokenizer(recipients, ";");

while (tokenizer.hasMoreTokens()) {

String recipient = tokenizer.nextToken();

message.addRecipient(Message.RecipientType.TO,

new InternetAddress(recipient));

}

transport.connect();

transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));

transport.close();

奇怪的是,每当我尝试使用main方法运行上述代码时,它都会成功发送SSL和TLS协议的电子邮件.

public static void main(String args[])

{

try {

Notifier.sendEmail("smtp.gmail.com", "587", "false", "true", "true","sender_email@gmail.com", "testpassword", "CHECKING SETTINGS", "CHECKING EMAIL FUNCTIONALITY", "text/html", "cc_email@gmail.com");

} catch (Exception ex) {

ex.printStackTrace();

}

}

但每当我尝试通过我的Web应用程序运行相同的代码时它就会失败.

通过SSL发送它会引发此错误:

com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication Required. Learn more at

jvm 1 | 530 5.5.1 https://support.google.com/mail/answer/14257 f12sm88286300pat.20 - gsmtp

jvm 1 |

jvm 1 | at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2057)

通过TLS发送会抛出此错误:

javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587;

jvm 1 | nested exception is:

jvm 1 | javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?

jvm 1 | at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)

任何形式的帮助表示赞赏.

EDIT1:

这是前端的tpl文件

Host:

Port:

Enable SSL?

Enable TLS?

Enable Authentication?

User:

Password:

Recipient(s):

值将保存在配置文件中,如下所示:

host=smtp.gmail.com

port=587

ssl=false

tls=true

auth=true

user=send_user_email@gmail.com

password=O0UbYboDfVFRaiA=

recipients=cc_user_email@gmail.com

trigger1=false

attempt=0

trigger2=false

percent=5

anyOrAll=ANY

trigger3=true

format=HTML

trigger4=true

trigger5=true

EDIT2:

public static void sendEmail(String message)

throws NoSuchProviderException, AddressException, MessagingException

{

if (message == null || message.trim().equals("")) return;

StringBuffer content = new StringBuffer();

content.append(getHeader());

content.append(message);

content.append(getFooter());

String format = NotifyProps.getFormat();

String type = "text/plain";

if (format.equals(NotifyProps.HTML)) type = "text/html";

sendEmail(NotifyProps.getHost(), NotifyProps.getPort(), Boolean.toString(NotifyProps.getUseAuth()), Boolean.toString(NotifyProps.getUseSSL()), Boolean.toString(NotifyProps.getUseTLS()),NotifyProps.getUser(), NotifyProps.getPassword(),

"Transaction Processor Auto Notification", content.toString(), type,

NotifyProps.getRecipients())

}

这是设置和获取属性的类:

谢谢.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaMail 是一个用于发送和接收电子邮件Java API。它可以用于实现移动邮件,即通过手机或其他移动设备发送和接收电子邮件。 要使用 JavaMail 实现移动邮件功能,首先需要配置相应的邮件服务器信息。可以使用 SMTP 协议来发送邮件,IMAP 或 POP3 协议来接收邮件。需要提供邮件服务器的地址、端口、用户名和密码等信息,以便 JavaMail 可以与邮件服务器进行通信。 发送邮件时,可以使用 MimeMessage 对象来创建一封邮件。需要设置邮件发送者、接收者、主题、正文内容等信息。还可以添加附件、设置邮件的优先级等。 接收邮件时,可以通过协议(IMAP 或 POP3)连接到邮件服务器,并使用相应的协议进行认证和获取邮件。可以使用 Folder 对象代表邮件文件夹,通过该对象可以获取邮件的数量、邮件的标志和状态等信息。可以使用 Message 对象来表示每封邮件,可以获取邮件发送者、接收者、主题、日期、内容等信息。 除了发送和接收邮件JavaMail 还提供了一些其他功能。例如,可以使用 SMTP 协议发送 HTML 邮件,可以使用附件来发送文件,可以使用 SSL/TLS 连接来保护邮件的安全性。 总之,JavaMail 是一个强大的工具,可以用于实现移动邮件。它提供了丰富的功能和灵活的配置选项,使开发者可以轻松地实现邮件发送和接收功能,并以此来实现移动邮件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值