jsp 登陆邮箱发送邮件

jsp 登陆邮箱发送邮件,第1张

这个需要用到mail.jar和activation.jar。 <%@ page contentType="text/htmlcharset=gb2312" language="java" import="java.sql.*" errorPage="../error.jsp" %><%@ page import="javax.mail.* ,javax.mail.internet.*" %><%@ page import="javax.activation.* ,java.util.*" %><% request.setCharacterEncoding("gb2312") String strHost="smtp.163.com"int nPort=0String strUserName="name"// 登陆名 String strPassWord="passwd"//密码 String strFrom="name@163.com"//邮箱 String strMessage="message"//邮件内容 Properties props= new Properties()props.put("mail.smtp.host",strHost)props.put("mail.smtp.auth","true")Session MailSession=Session.getDefaultInstance(props,null)MailSession.setDebug(true)//创建一个消息 Message msg=new MimeMessage(MailSession)//发件人 InternetAddress from=new InternetAddress(strFrom)msg.setFrom(from)//目标地址 if(strToList!=null &&strToList.length()>0) { InternetAddress[] to= {new InternetAddress(strToList)}msg.setRecipients(Message.RecipientType.TO,to)} else {throw new Exception("No Recipient!")} if(strSubject!=null) { msg.setSubject(strSubject)} //msg.setSentDate(new Date())if(strMessage!=null &&strMessage.trim().length()>0) { msg.setText(strMessage)} else {msg.setText("NO message to be sent!") } msg.saveChanges()//报存更改信息 int nMailPort=-1if(nPort >0) nMailPort=nPort Transport transport=MailSession.getTransport("smtp")transport.connect(strHost,nMailPort,strUserName,strPassWord)transport.sendMessage(msg,msg.getAllRecipients())transport.close()out.print("邮件发送成功!") }catch(AuthenticationFailedException ae) { ae.printStackTrace()out.print("error")} catch(SendFailedException sf) {Address[] invalid=sf.getInvalidAddresses()Address[] valid=sf.getValidSentAddresses()Address[] unsend=sf.getValidUnsentAddresses()sf.printStackTrace()} catch(Exception e) {e.printStackTrace()} %>

记得采纳啊

这里面要加载javamail包 去网上找能找到的! activation.jar cos.jar mail.jarindex.jsp <%@ page contentType="text/htmlcharset=gb2312" language="java" errorPage="../error/error.jsp" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " http://www.w3.org/TR/html4/loose.dtd"><html><head><title>发送电子邮件</title><meta http-equiv="Content-Type" content="text/htmlcharset=gb2312"><link rel="stylesheet" type="text/css" href="../image/com.css"/></head><body> <form method="post" name="form1" action="sendmail.jsp" onSubmit="return email()"><table width="480" height="393" border="1" align="center" cellpadding="0" cellspacing="0"><tr bordercolor="#D4D0C8" bgcolor="#FEFFC9"> <td height="27" colspan="2" align="left"><div align="center"><strong>发送电子邮件</strong></div></td></tr><tr bgcolor="#FEFFC9"> <td width="68" height="27" align="left"><div align="right">收件人:</div></td><td width="406" height="27"><div align="center"> <input type="text" name="sname" size="51"></div></td></tr><tr bgcolor="#FEFFC9"> <td height="27" align="left"><div align="right">发件人:</div></td><td height="27"><div align="center"> <input type="text" name="jname" size="51"></div></td></tr><tr bgcolor="#FEFFC9"> <td height="27" align="left"><div align="right">密  码:</div></td><td height="27"><div align="center"><input type="password" name="password" size="57"></div></td></tr><tr bgcolor="#FEFFC9"> <td height="27" align="left"><div align="right">主  题:</div></td><td height="27"><div align="center"> <input name="title" type="text" size="51"></div></td></tr><tr align="left" bgcolor="#FEFFC9"> <td height="227" valign="top"><div align="right">内  容:</div></td><td height="227"><div align="center"> <textarea name="message" cols="50" rows="15"></textarea></div></td></tr><tr align="center" valign="middle" bordercolor="#D4D0C8" bgcolor="#FEFFC9"> <td height="29" colspan="2"><input type="submit" name="Submit" value="发送">    <input type="reset" name="Submit2" value="清除"></td></tr></table></form></body></html> sendmail.jsp(邮件发送!) <%@ page contentType="text/htmlcharset=gb2312" language="java" errorPage="" %><%@ page import="java.io.*" %><%@ page import="java.util.*" %><%@ page import ="javax.mail.*" %><%@ page import="javax.mail.internet.*" %><%@ page import="javax.activation.*" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns=" http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/htmlcharset=gb2312" /><title>发送邮件</title></head><link rel="stylesheet" type="text/css" href="../image/com.css"/><% try{ request.setCharacterEncoding("gb2312")String from=request.getParameter("jname")String to=request.getParameter("sname")String subject=request.getParameter("title")String messageText=request.getParameter("message")String password=request.getParameter("password")String S =request.getParameter("jname")int n =S.indexOf('@')int m=S.length() String server =S.substring(n+1,m)//建立邮件会话 Properties pro=new Properties()pro.put("mail.smtp.host","smtp."+server)pro.put("mail.smtp.auth","true")Session sess=Session.getInstance(pro)sess.setDebug(true)//新建一个消息对象 MimeMessage message=new MimeMessage(sess)//设置发件人 InternetAddress from_mail=new InternetAddress(from)message.setFrom(from_mail)//设置收件人 InternetAddress to_mail=new InternetAddress(to)message.setRecipient(Message.RecipientType.TO ,to_mail)//设置主题 message.setSubject(subject)//设置内容 message.setText(messageText)//设置发送时间 message.setSentDate(new Date())//发送邮件 message.saveChanges()//保存邮件信息 Transport transport =sess.getTransport("smtp")// transport.connect("smtp."+server,from,password)//这一句很关键,相当于登陆管理员邮箱来发邮件 // transport.connect("smtp.163.com",wode@163.com,"123456")transport.connect(" smtp.qq.com ",from,password)transport.sendMessage(message,message.getAllRecipients())transport.close()out.print("发送邮件成功")}catch(Exception e){ out.print("发送邮件失败,原因可能是")out.println(e.getMessage())} %></html>

实现在jsp页面中发送邮件,可以结合Servlet来实现,步骤如下:

(1)、先在项目WebRoot--->META-INF文件夹下面建立一个名为:context.xml文件(context文件名是固定的),接下就是在context.xml中进行配置,配置如下:

<Context>//Context头字母需是大写

<Resource name="mail/Session" auth="Container"

type="javax.mail.Session"

//配置邮件服务器主机,以及设置与服务器进行会话要进行身份认证

mail.smtp.host="smtp.sina.com" mail.smtp.auth="true"

//设置邮件传输的协议

mail.transport.protocol="smtp"

/>

</Context>

(2)、需要把mail.jar放入tomact中lib中去,记住不要把mail.jar放入项目中的lib中去否则会出错

(3)、新建一个Servlet类,使用doPost()方法进行邮件的处理,doPost()方法中的代码如下:

//创建一个InitialContext对象来初始化Context

Context initCtx=new InitialContext()

Context envCtx=(Context)initCtx.lookup("java:com/env")

//读取服务器的环境信息

Session session=(Session)initCtx.lookup("mail/session")

//创建一个message对象,用来创建一封邮箱

Message message=new MimeMessage(session)

//设置邮件发送者邮箱地址

message.setForm(new InternetAddress("*****@sina.com"))

//设置邮件接收者们的邮箱地址,假设只发送给一个人

InternetAddress[] to=new InternetAddress[1]

to[0]=new InternetAddress("******@qq.com")

//设置邮件发送的类型

message.setRecipients(Message.Recipients.to,to)

//邮件标题

message.setSubject("春节快乐")

//邮件正文

message.setText("祝新的一年里家人和谐快乐")

//创建一个传输对象

Transport transport=session.getTransport()

//建立与服务器的链接

transport.connect("smtp.sina.com",25,"****@sina.com","*******")

//发送邮件

transport.sendMessage(messgae,to)

//关闭网邮件传输

transport.close()

希望能够帮助到您,望君采纳哟


欢迎分享,转载请注明来源:夏雨云

原文地址:https://www.xiayuyun.com/zonghe/776848.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-08-22
下一篇2023-08-22

发表评论

登录后才能评论

评论列表(0条)

    保存