Properties props = new Properties
(); props.put("mail.smtp.host", "smtp.xxxx.com");//可以換上你的smtp主機(jī)名,就像你在OutLook中設(shè)置smtp主機(jī)名一樣。 |
Session session = Session.getInstance(props, null); |
MimeMessage msg = new MimeMessage(session); |
Transport.send(msg); |
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class SendMail extends HttpServlet implements SingleThreadModel{ public void init(ServletConfig Conf) throws ServletException { super.init(Conf); } public void doPost(HttpServletRequest Req, HttpServletResponse Res) throws ServletException, IOException { try{ //實(shí)例化剛才我們做的類,并按其構(gòu)造傳進(jìn)相應(yīng)的參數(shù) Mail sendmail=newMail("zhang@263.net", "chtwoy@21cn.com","smtp.21cn.com","test"); sendmail.attachfile("table.pdf"); sendmail.startSend(); }catch(Exception e){ e.printStackTrace(); } } public void destroy() { } } |