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()} %>

记得采纳啊

import javax.mail.*

//需要JAVAMAIL或J2EE

import javax.mail.internet.*

import java.util.*

import javax.activation.*

//耐滚需要JAF支持

public class sendmailbean

{

private String from

private String subject

private String toaddress

private String text

private String mailhost

private String result

public sendmailbean()

{

from=null

subject=null

toaddress=null

text=null

mailhost="127.0.0.1"

result=null

}

public void setfrom(String s)

{

from=s

}

public void setsubject(String s)

{

subject=s

}

public void settoaddress(String s)

{

toaddress=s

}

public void settext(String s)

{

text=s

}

public void setmailhost(String s)

{

mailhost=s

System.out.println ("setmailhost: "+s)

}

public String getfrom()

{

return from

}

public String getmailhost()

{

return mailhost

}

public String getsubject()

{

return subject

}

public String gettext()

{

return text

}

public String gettoaddress()

{

return toaddress

}

public String sendmail()

//发送邮件并返回结果result

{

try {

java.util.Properties p=new Properties()

p.put("mail.transport.smtp",mailhost)

//和磨设置邮件服务器为本机。

javax.mail.Session s=Session.getInstance(p,null)

Message msg=new MimeMessage(s)

msg.setFrom(new javax.mail.internet.InternetAddress(from))

//设置发信人地址

msg.setSubject(subject)

//设置主题

javax.mail.internet.InternetAddress mailtoaddress=new InternetAddress()

mailtoaddress.setAddress(toaddress)

//设置收信人地址

msg.setRecipient(Message.RecipientType.TO,mailtoaddress)

msg.setText(text)

//设置内容

Transport trans=s.getTransport("smtp")

trans.send(msg)//发送信件

result="邮件发送成功"//设置返回信息为成功

}

catch (Exception ex) {

result="邮件发送失败: "+ex.toString()//发生异常时设置返回信息为失败并显示异常

}

return result

}

}

上面是bean.

下面是jsp页面.

<%@ page contentType="text/htmlcharset=gb2312" %>

<%@ page import="java.util.*"%>

<jsp:useBean id="mail" scope="application" class="xue.mail.sendmailbean"/>

<%! String from=null%>

<%! String subject=null%>

<%! String toaddress=null%>

<%! String text=null%>

<%! String result=null%>

<html>

<head><title>发送唤亩斗邮件</title></head>

<body>

正在发送邮件....<BR>

<%

text=new String(request.getParameter("text").getBytes("ISO8859_1"))

subject=new String(request.getParameter("subject").getBytes("ISO8859_1"))

from=request.getParameter("from")

toaddress=request.getParameter("toaddress")

mail.setfrom(from)

mail.settoaddress(toaddress)

mail.settext(text)

mail.setsubject(subject)

result=mail.sendmail()

out.println(result)

%>

</body>

</html>

最下面是html表单.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML>

<HEAD>

<TITLE>发送邮件</TITLE>

</HEAD>

<BODY>

<FORM METHOD=POST ACTION="sendmail.jsp">

收信人:<INPUT TYPE="text" NAME="toaddress"><BR>

发信人:<INPUT TYPE="text" NAME="from"><BR>

主 题:<INPUT TYPE="text" NAME="subject"><BR>

内 容:<BR>

<TEXTAREA NAME="text" ROWS="6" COLS="27"></TEXTAREA><BR>

<INPUT TYPE="submit">

</FORM>

</BODY>

</HTML>

全都放到你服务器上正确位置,再改动bean中的mailhost为你的邮件服务器.

就可以用了,


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12311649.html

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

发表评论

登录后才能评论

评论列表(0条)

保存