怎么实现 ssh发送邮件  请高手帮忙解决一下

解决方案 »

  1.   

     好像和ssh没有关系的吧  昨天有人问过。。http://topic.csdn.net/u/20101005/23/98440322-16dc-4bcd-bc77-c7c4f02dcedd.html 参考下。。
      

  2.   

    我用的javamail:package com.adp.util;import java.io.File;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Properties;import javax.activation.DataHandler;
    import javax.activation.DataSource;
    import javax.activation.FileDataSource;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Multipart;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.AddressException;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeBodyPart;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMultipart;
    import javax.mail.internet.MimeUtility;public class MailUtil { private String host; private String from; private String to; private String subject; private String content; private boolean authentication; private String username; private String password;

    private boolean  isAttachement = false;  //是否有附件

    //private String[] fileNames;  //附件名称

    public MailUtil() {};


    public void sendMail(String host, String from, String to, String subject,
    String content, boolean authentication, String username,
    String password,boolean isAttachement,List fileNames) throws MessagingException {
    // Get system properties
    //Properties props = System.getProperties();
    Properties props = new Properties();
    // Setup mail server
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.port", 25);
    if (!authentication) {
    props.put("mail.smtp.auth", "false");
    } else {
    props.put("mail.smtp.auth", "true");
    }
    // Get session
    //Session session = Session.getDefaultInstance(props, null);
    Session session = Session.getInstance(props);
    // Define message
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
    message.setSubject(subject);
     


    Multipart multipart = new MimeMultipart();

     
    //邮件正文
    MimeBodyPart mbp1 = new MimeBodyPart();
            mbp1.setText(content);
            multipart.addBodyPart(mbp1);
            
            //附件发送
    if(isAttachement){
    this.addAttachement(fileNames, message, multipart);
    }  //邮件内容
            message.setContent(multipart);

    // Send message
    if (authentication) {
    Transport smtp = null;
    try {
    smtp = session.getTransport("smtp");
    smtp.connect(host, username, password);
    smtp.sendMessage(message, message.getAllRecipients());
    } catch (AddressException e) {
    e.printStackTrace();
    } catch (MessagingException e) {
    e.printStackTrace();
    } finally {
    smtp.close();
    }
    } else {
    Transport.send(message);
    }
    }

      //添加附件
    public void addAttachement(List fileNames,MimeMessage message,Multipart multipart){

    for(int i = 0;i < fileNames.size();i++){
    try {
    String fileAttachment = fileNames.get(i).toString();
    MimeBodyPart messageBodyPart = new MimeBodyPart();
    DataSource source =  new FileDataSource(fileAttachment);

    messageBodyPart.setDataHandler(new DataHandler(source));
     //设置文件名称
    String newFileName = fileAttachment.substring(fileAttachment.lastIndexOf("/")+1);
     //解决文件名乱码
    messageBodyPart.setFileName(MimeUtility.encodeText(newFileName));
    multipart.addBodyPart(messageBodyPart); // Put parts in message

    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
      

    }

     /**
      * 带附件的发送方法
      * @param to
      * @param subject
      * @param content
      * @param fileNames
      */
    public String send(String to,String subject,String content,List fileNames) {

    host = "mail.mediaat.net";
    //from = "[email protected]";
    //to = "[email protected]";
    //subject = "just for test";
    //content = "http://comment2.news.sohu.com";

    from = "[email protected]";

    this.to = to;

    this.content = content;

    this.subject = subject;

    authentication = true;

    //username = "[email protected]";
    //password = "8810701";

    username = "[email protected]";
    password = "123456";

    //String[] fieleNames = {"E:/图片素材/1.jpg","E:/图片素材/3.docx"};
    try {
    sendMail(host, from, this.to, this.subject,this.content, authentication, username,
    password,true,fileNames);
    System.out.println("发送成功");
    return "发送成功";
    } catch (Exception e) {
    System.out.println("mail");
    e.printStackTrace();
    return "发送失败";
    }

    }  /**
      * 不带附件的发送发
      * @param to
      * @param subject
      * @param content
      */
         public String send(String to,String subject,String content) {

    host = "mail.mediaat.net";
    //from = "[email protected]";
    //to = "[email protected]";
    //subject = "just for test";
    //content = "http://comment2.news.sohu.com";

    from = "[email protected]";

    this.to = to;

    this.content = content;

    this.subject = subject;

    authentication = true;

    //username = "[email protected]";
    //password = "8810701";

    username = "[email protected]";
    password = "123456";

    //String[] fieleNames = {"E:/图片素材/1.jpg","E:/图片素材/3.docx"};
    try {
    sendMail(host, from, this.to, this.subject,this.content, authentication, from,
    password,false,new ArrayList());
    System.out.println("发送成功");
    return "发送成功";
    } catch (MessagingException e) {
    System.out.println("mail");
    e.printStackTrace();
    return "发送失败";
    }

    } public static void main(String args[]) throws MessagingException {
     
     List fileNames = new ArrayList();
     fileNames.add("d:/图片素材/1.jpg");
     fileNames.add("E:/图片素材/我的文档.docx");
     MailUtil jm = new MailUtil();
     jm.send("[email protected]","111","222",fileNames);

    }}
      

  3.   

    请百度搜索spring发送邮件,刚弄过