java gmail smtp_使用Javamail连接到Gmail smtp服务器会忽略指定的端口,并尝试使用25...

我正在尝试在groovy脚本中使用javamail通过gmail发送电子邮件。我在网上看了很多地方,到目前为止还无法正常工作。运行脚本时出现的错误是:

DEBUG SMTP: useEhlo true, useAuth false

DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 25, isSSL false

Caught: javax.mail.SendFailedException: Send failure (javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 25 (javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?))

即使我已指定应使用端口587,它似乎仍在尝试使用端口25。是否有人知道可能导致此问题的原因,我已经使用telnet连接到端口587上的smtp服务器,并且雷鸟使用端口587具有STARTTLS安全性,并且能够使用smtp服务器成功发送邮件。这告诉我这不是端口或连接问题。这是我用来尝试发送电子邮件的代码:

import javax.mail.*

import javax.mail.internet.*

private class SMTPAuthenticator extends Authenticator

{

public PasswordAuthentication getPasswordAuthentication()

{

return new PasswordAuthentication('email@gmail.com', 'password');

}

}

def  d_email = "email@gmail.com",

d_password = "password",

d_host = "smtp.gmail.com",

d_port  = "587", //465,587

m_to = "email@gmail.com",

m_subject = "Testing",

m_text = "This is a test."

def props = new Properties()

props.put("mail.smtp.user", d_email)

props.put("mail.smtp.host", d_host)

props.put("mail.smtp.port", d_port)

props.put("mail.smtp.starttls.enable","true")

props.put("mail.smtp.debug", "true");

props.put("mail.smtp.auth", "true")

props.put("mail.smtp.socketFactory.port", d_port)

props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory")

props.put("mail.smtp.socketFactory.fallback", "false")

def auth = new SMTPAuthenticator()

def session = Session.getInstance(props, auth)

session.setDebug(true);

def msg = new MimeMessage(session)

msg.setText(m_text)

msg.setSubject(m_subject)

msg.setFrom(new InternetAddress(d_email))

msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to))

Transport.send(msg)

任何帮助将不胜感激。提前致谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用JavaMail获取邮件附件内容,需要进行以下步骤: 1. 导入JavaMailJava Activation Framework库。 2. 创建Session对象,设置SMTP服务器端口号。 ```java Properties props = new Properties(); props.setProperty("mail.smtp.host", "smtp.gmail.com"); props.setProperty("mail.smtp.port", "587"); props.setProperty("mail.smtp.auth", "true"); props.setProperty("mail.smtp.starttls.enable", "true"); Session session = Session.getDefaultInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("yourusername@gmail.com", "yourpassword"); } }); ``` 3. 创建IMAPStore对象,连接到邮箱。 ```java Store store = session.getStore("imap"); store.connect("imap.gmail.com", "yourusername@gmail.com", "yourpassword"); ``` 4. 获取邮件夹对象,打开邮件夹。 ```java Folder inbox = store.getFolder("INBOX"); inbox.open(Folder.READ_ONLY); ``` 5. 遍历邮件夹中的邮件,获取附件。 ```java Message[] messages = inbox.getMessages(); for (int i = 0; i < messages.length; i++) { Message message = messages[i]; String contentType = message.getContentType(); if (contentType.contains("multipart")) { Multipart multiPart = (Multipart) message.getContent(); for (int j = 0; j < multiPart.getCount(); j++) { MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(j); if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) { String fileName = part.getFileName(); InputStream inputStream = part.getInputStream(); // 读取附件内容 // ... } } } } ``` 6. 关闭邮件夹和IMAPStore对象。 ```java inbox.close(false); store.close(); ``` 以上就是使用JavaMail获取邮件附件内容的基本步骤。需要注意的是,不同邮件服务器的设置可能有所不同,需要根据实际情况进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值