腾讯企业邮箱阿里云端口25发送失败

编程入门 行业动态 更新时间:2024-10-06 12:22:05

<a href=https://www.elefans.com/category/jswz/34/1770070.html style=腾讯企业邮箱阿里云端口25发送失败"/>

腾讯企业邮箱阿里云端口25发送失败

写一个企业邮箱发送的问题,每个用户都能发送邮件,这个还是比较好写的,百度一大推,但是我是在本地测试的,我把代码部署到阿里云上去之后,一直发送失败,查看日志文件,说连接腾讯邮箱服务器失败,端口使用的是25,后面经过资料查找,说什么阿里云不支持25端口,要使用ssl的465.后面经过测试果然ok.记录一下.

每个用户都能够发送邮件,为了密码安全,全部在腾讯企业邮箱设置客户端安全密码

@Data
public class MailBean {private String[] to; // 收件人private String[] cc;//抄送人private String from; // 发件人private String host; // SMTP主机private String username; // 发件人的用户名private String password; // 发件人的密码private String subject; // 邮件主题private String content; // 邮件正文private InputStream fileInputStream; // 文件流private String filename; // 附件的文件名}public boolean sendMail(MailBean mb) throws IOException {String host = mb.getHost();final String password = mb.getPassword();final String from = mb.getFrom();String[] to = mb.getTo();String[] cc=mb.getCc();String subject = mb.getSubject();String content = mb.getContent();String fileName = mb.getFilename();InputStream file = mb.getFileInputStream();Properties prop = new Properties();//协议prop.setProperty("mail.transport.protocol", "smtp");//服务器prop.setProperty("mail.smtp.host", "smtp.exmail.qq");//端口prop.setProperty("mail.smtp.port", "465");//使用smtp身份验证prop.setProperty("mail.smtp.auth", "true");MailSSLSocketFactory sf = null;try {sf = new MailSSLSocketFactory();sf.setTrustAllHosts(true);} catch (GeneralSecurityException e1) {e1.printStackTrace();}prop.put("mail.smtp.ssl.enable", "true");prop.put("mail.smtp.ssl.socketFactory", sf);Session session = Session.getDefaultInstance(prop, new Authenticator() {public PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(from, password);}});try {MimeMessage msg = new MimeMessage(session);msg.setFrom(new InternetAddress(from));InternetAddress[] toAddress = new InternetAddress[to.length];//添加收件人for(int i=0;i<to.length;i++){toAddress[i]=new InternetAddress(to[i]);}InternetAddress[] ccAddress = new InternetAddress[cc.length];//添加抄送人for(int i=0;i<cc.length;i++){ccAddress[i]=new InternetAddress(cc[i]);}msg.setRecipients(Message.RecipientType.TO, toAddress);msg.setRecipients(Message.RecipientType.CC, ccAddress);msg.setSubject(toChinese(subject));Multipart mp = new MimeMultipart();MimeBodyPart body = new MimeBodyPart();body.setContent(content, "text/html; charset=UTF-8");mp.addBodyPart(body);/* 往邮件中添加附件 */if (file != null) {MimeBodyPart mbpFile = new MimeBodyPart();DataSource dataSource=new ByteArrayDataSource(file, "application/png");DataHandler dataHandler=new DataHandler(dataSource);;mbpFile.setDataHandler(dataHandler);mbpFile.setFileName(fileName);mp.addBodyPart(mbpFile);}msg.setContent(mp);msg.setSentDate(new Date());Transport.send(msg);} catch (MessagingException me) {me.printStackTrace();logger.error(me.getMessage());return false;}return true;}

记录一下!

更多推荐

腾讯企业邮箱阿里云端口25发送失败

本文发布于:2024-03-10 19:39:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1728865.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:腾讯   阿里   云端   企业邮箱

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!