java mail 端口号_使用Javamail连接到Gmail smtp服务器将忽略指定的端口并尝试使用25...

我试图使用javamail在groovy脚本通过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?))

似乎是尝试使用端口25,即使我已经指定它应该使用端口587.有谁知道可能是什么导致这个问题,我使用telnet连接到端口587的smtp服务器,和thunderbird使用端口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)

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

-Bryan

使用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、付费专栏及课程。

余额充值