栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用JSP发送电子邮件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用JSP发送电子邮件

好吧,我会说尝试连接时存在身份验证问题。您不提供任何用户名或密码,除非您的交换服务器不需要用户名和密码。


更新:如果使用JDK 7,请参阅以下文章,它解决了此问题:

缺陷-使用VPN时,套接字拒绝JDK7权限

“更多地挖掘,似乎VPN客户端已禁用IPv6,这导致JDK7出现问题。如果我使用以下标志-Djava.net.preferIPv4Stack =
true,我将不再看到错误。这是预期的解决方法还是这是一个问题?


public class MailTest {        public static void main(String[] args) throws MessagingException {        String host = "smtp.gmail.com";        String to = "myEmail@gmail.com";        String from = "myEmail@gmail.com";        String subject = "test";        String messageText = "body test";        Properties props = System.getProperties();        props.put("mail.host", host);        props.put("mail.transport.protocol", "smtp");        props.put("mail.smtp.port", "25");        // If using authentication, otherwise comment out        props.put("mail.smtp.auth", "true");        // Gmail requires TLS, your server may not        props.put("mail.smtp.starttls.enable", "true");        Session mailSession = Session.getDefaultInstance(props, null);        Message msg = new MimeMessage(mailSession);        msg.setFrom(new InternetAddress(from));        InternetAddress[] address = {new InternetAddress(to)};        msg.setRecipients(Message.RecipientType.TO, address);        msg.setSubject(subject);        msg.setSentDate(new Date());        msg.setText(messageText);        Transport transport = mailSession.getTransport("smtp");        //connect with authentication        //transport.connect(host,"myUsername" , "myPassword");        //connect without authentication        transport.connect();        transport.sendMessage(msg, address);        transport.close();        System.out.println("Mail was sent to " + to);        System.out.println(" from " + from);        System.out.println(" using host " + host + ".");    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/435616.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号