java smtp发送邮件
2013-08-14 16:25
387 查看
public String sendPostbox(){ //获得属性,并生成Session对象 PostboxSite postboxSite = this.postboxManager.getPostboxSiteByMail(this.sendBoxForm.getFromUserName()); try { Properties props = new Properties(); Session sendsession; Transport transport; sendsession = Session.getInstance(props, null); //向属性中写入SMTP服务器的地址 props.put("mail.smtp.host", "smtp."+"163.com"); //设置SMTP服务器需要权限认证 props.put("mail.smtp.auth","true"); //设置输出调试信息 sendsession.setDebug(true); //根据Session生成Message对象 Message message = new MimeMessage(sendsession); //设置发信人地址 message.setFrom(new InternetAddress("xxxxxx@163.com")); //设置收信人地址 message.setRecipient(Message.RecipientType.TO,new InternetAddress("xxxxxxxx@qq.com")); //设置e-mail标题 message.setSubject("aabbccdd"); //设置e-mail发送时间 message.setSentDate(new Date()); //设置e-mail内容 message.setText("youjian"); //获得attachment参数 String attachment = new String(""); if (!attachment.equals("")) { }else{ //如无附件,则按纯文本格式处理 message.setText(new String("我的第一封邮件")); } //保存对于e-mail的修改 message.saveChanges(); //根据Session生成Transport对象 transport=sendsession.getTransport("smtp"); //连接到SMTP服务器 transport.connect("smtp."+"163.com","xxxxx","xxxxxx");//smtp服务器,邮箱用户名,邮箱密码 //发送e-mail transport.sendMessage(message,message.getAllRecipients()); //关闭Transport连接 transport.close(); //保存发送的邮件信息 SendBox sendBox = new SendBox(); sendBox.setMailTitle(this.sendBoxForm.getMailTitle()); sendBox.setToUserNames(this.sendBoxForm.getToUserNames()); sendBox.setFromUserName(this.sendBoxForm.getFromUserName()); sendBox.setMailContent(this.sendBoxForm.getMailContent()); } catch (AddressException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } ActionContext ctx = ActionContext.getContext(); ctx.put(WebConstants.STRUTS_SUBMIT_KEY, Boolean.TRUE); return WebConstants.ACTION_SUCCESS_KEY; }
相关文章推荐
- java通过SMTP发送QQ邮件的完全步骤
- java实现基于SMTP发送邮件的方法
- java smtp.sina.com发送邮件解决及编码修正!
- JAVA-SMTP发送邮件
- java smtp 邮箱向外部发送邮件 550 5.7.1 Unable to relay
- java通过SMTP发送QQ邮件的完全步骤
- java通过smtp发送qq邮件详细步骤
- 转贴:怎么用java发送邮件一及通过smtp的认证
- 使用java通过smtp完成发送邮件源码
- 【Java】纯底层SMTP实现邮件发送核心源码
- 再谈用java实现Smtp发送邮件之Socket编程
- java spring 开启SMTP服务发送QQ邮件
- 用Java发送邮件 smtp验证码问题
- java smtp 发送邮件的例子
- 【转】javamail-基于SMTP的JAVA邮件发送程序!
- JAVA-SMTP发送邮件
- java网络编程三:基于SMTP的邮件发送客户端
- java实现基于SMTP发送邮件的方法
- java使用smtp发送邮件问题
- 基于SMTP的JAVA邮件发送程序!