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

使用Javamail API将邮件从yahoo ID发送到其他电子邮件ID

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

使用Javamail API将邮件从yahoo ID发送到其他电子邮件ID

试试这个代码

public class SendMail {    String host, port, emailid,username, password;    Properties props = System.getProperties();    Session l_session = null;    public BSendMail() {        host = "smtp.mail.yahoo.com";        port = "587";        emailid = "a@yahoo.com";        username = "a";        password = "pwd";        emailSettings();        createSession();        sendMessage("a@yahoo.com", "rahul@gmail.com","Test","test Mail");    }    public void emailSettings() {        props.put("mail.smtp.host", host);        props.put("mail.smtp.auth", "true");        props.put("mail.debug", "false");        props.put("mail.smtp.port", port);//        props.put("mail.smtp.socketFactory.port", port);//        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");//        props.put("mail.smtp.socketFactory.fallback", "false");    }    public void createSession() {        l_session = Session.getInstance(props,     new javax.mail.Authenticator() {         protected PasswordAuthentication getPasswordAuthentication() {  return new PasswordAuthentication(username, password);         }     });        l_session.setDebug(true); // Enable the debug mode    }    public boolean sendMessage(String emailFromUser, String toEmail, String subject, String msg) {        //System.out.println("Inside sendMessage 2 :: >> ");        try { //System.out.println("Sending Message *********************************** "); MimeMessage message = new MimeMessage(l_session); emailid = emailFromUser; //System.out.println("mail id in property ============= >>>>>>>>>>>>>> " + emailid); //message.setFrom(new InternetAddress(emailid)); message.setFrom(new InternetAddress(this.emailid)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail)); message.addRecipient(Message.RecipientType.BCC, new InternetAddress(AppConstants.fromEmail)); message.setSubject(subject); message.setContent(msg, "text/html"); //message.setText(msg); Transport.send(message); System.out.println("Message Sent");        } catch (MessagingException mex) { mex.printStackTrace();        } catch (Exception e) { e.printStackTrace();        }//end catch block        return true;    }}


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

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

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