⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mailbean.java

📁 用java实现的一个bbs的portal
💻 JAVA
字号:
package Mail;

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

public class MailBean {

    public final String propertyfile="mail_properties.ini";

    
    private static String smtpserver = null;
    private String sender = "";
    private String receiver = "";
    private String subject = "";
    private String content = "";

    public MailBean()
    {
        if (smtpserver == null)
            initSMTP();
    }

    public String initSMTP()
    {
        Vector vec=new Vector();
        try{
            BufferedReader reader=new BufferedReader(new FileReader(propertyfile));
            String str=null;
            while ((str=reader.readLine())!=null){
                vec.add(str);
            }
            reader.close();
        }
        catch (Exception e) {
            System.err.println(new Date() + ": " + "ERROR:Can not read file "+propertyfile+" !");
            e.printStackTrace();
            return null;
        }
        smtpserver = (String)vec.get(0);
        smtpserver = smtpserver.trim();
        return smtpserver;
    }

    public void setContent(String from,String to,String subject,String text){
        this.sender=from;
        this.receiver=to;
        this.subject=subject;
        this.content=text;
    }

    public boolean sendMail()
    {
        Properties props = new Properties();
        Session sendMailSession;
        Transport transport;

        try{
            if (smtpserver==null)
            {
                System.err.println(new Date() + ": " + "ERROR:The SMTP server address is wrong !");
                return false;
            }
            props.put("mail.host",smtpserver);
            props.put("mail.transport.protocol","smtp");
            props.put("mail.smtp.host",smtpserver); //smtp server address
            sendMailSession = Session.getDefaultInstance(props);
            MimeMessage newMessage = new MimeMessage(sendMailSession);
            newMessage.setFrom(new InternetAddress(sender));
            newMessage.setRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(receiver));
            newMessage.setSubject(subject);
            newMessage.setSentDate(new Date());
            newMessage.setText(content);
            transport = sendMailSession.getTransport();
            transport.send(newMessage);
            return true;
        }
        catch(Exception e)
        {
            e.printStackTrace();
            return false;
        }
    }

    public static void main(String[] args) {
        MailBean mail = new MailBean();
        mail.setContent("letter@bbs.pku.edu.cn","wanglei@ebusiness.pku.edu.cn","haha","fuck");
        mail.sendMail();
    }



}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -