欢迎光临
我们一直在努力

修改后可以发送附件、抄送、密送的javabean,吐血推荐~(javamail范例)-JSP教程,邮件相关

建站超值云服务器,限时71元/月

/*
*author:tyfun
*datetime:2003.01.09
*package:com.westarsoft.function
*/

package com.westarsoft.function;

import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class sendmail {
    private string smtpserver = new string();
    private string from = new string();
    private string subject = new string();
    private string content = new string();
    private string user = new string();
    private string password = new string();
    private string fileattachment = new string();
    public string getsmtpserver() {
        return smtpserver;
    }
    public void setsmtpserver(string smtpserver) {
        this.smtpserver = smtpserver;
    }
    public string getfrom() {
        return from;
    }
    public void setfrom(string from) {
        this.from = from;
    }
    public string getsubject() {
        return subject;
    }
    public void setsubject(string subject) {
        this.subject = subject;
    }
    public string getcontent() {
        return content;
    }
    public void setcontent(string content) {
        this.content = content;
    }
    public string getuser() {
        return user;
    }
    public void setuser(string user) {
        this.user = user;
    }
    public string getpassword() {
        return password;
    }
    public void setpassword(string password) {
        this.password = password;
    }
    public string getfileattachment() {
        return fileattachment;
    }
    public void setfileattachment(string fileattachment) {
        this.fileattachment = fileattachment;
    }    

    public void sendmailto(string to,string cc,string bcc) {
        try {
            properties props = new properties();
            session sendmailsession;
            store store;
            transport transport;
            
            sendmailsession = session.getinstance(props, null);
            props.put("mail.smtp.host", smtpserver);
            mimemessage newmessage = new mimemessage(sendmailsession);
            newmessage.setfrom(new internetaddress(from));
             newmessage.setsubject(subject);
            newmessage.setsentdate(new date());
            /*
            stringtokenizer tokento = new stringtokenizer(to, ",");        
            internetaddress[] addrarrto = new internetaddress[tokento.counttokens()];
            int i = 0;
            while(tokento.hasmoretokens()) {
                addrarrto[i] = new internetaddress(tokento.nexttoken().tostring());
                i++;
            }
            */            
            transport = sendmailsession.gettransport("smtp");
            transport.connect(smtpserver, user, password);
            //newmessage.setrecipients(message.recipienttype.to, addrarrto);
            newmessage.setrecipients(message.recipienttype.to, internetaddress.parse(to));
            newmessage.setrecipients(message.recipienttype.cc, internetaddress.parse(cc));
            newmessage.setrecipients(message.recipienttype.bcc, internetaddress.parse(bcc));
            
            mimebodypart messagebodypart = new mimebodypart();
            messagebodypart.settext(content);
            multipart multipart = new mimemultipart();
            multipart.addbodypart(messagebodypart);
            messagebodypart = new mimebodypart();
            datasource source = new filedatasource(fileattachment);
            messagebodypart.setdatahandler(new datahandler(source));
            messagebodypart.setfilename(fileattachment);
            multipart.addbodypart(messagebodypart);
            newmessage.setcontent(multipart);
                    
            transport.send(newmessage);
        }
        catch(exception e) {
            system.out.println(e);
        }
    }
}

<%
if(request.getmethod().equals("post")) {
    sendmail mail = new sendmail();
    mail.setsmtpserver("200.1.1.157");
    mail.setuser("lint");
    mail.setpassword("30320");
    mail.setfrom(request.getparameter("from"));
    mail.setsubject(request.getparameter("subject"));
    mail.setcontent(request.getparameter("content"));
    mail.setfileattachment(request.getparameter("filename"));
    mail.sendmailto(request.getparameter("to"),request.getparameter("cc"),request.getparameter("bcc"));
}
%>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 修改后可以发送附件、抄送、密送的javabean,吐血推荐~(javamail范例)-JSP教程,邮件相关
分享到: 更多 (0)

相关推荐

  • 暂无文章