【问题标题】:Redirect bounced mails, and read the attached filename from the bounced mail重定向退回的邮件,并从退回的邮件中读取附加的文件名
【发布时间】:2015-07-06 14:54:29
【问题描述】:

我有 2 个程序,一个用于发送邮件,另一个用于阅读邮件。我的第一个问题是如何将退回的邮件重定向到用户以外的电子邮件 ID。第二个问题是如果我有一封退回的邮件,我如何阅读附件的名称。我经历了很多帖子,但似乎没有任何效果。任何帮助表示赞赏。

用于发送邮件的代码

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class bounceMail {
    String host, port, emailid,username, password;
    Properties props = System.getProperties();
    Session l_session = null;
    Session l_session1= null;

    public void sendMessage(String toEmail, String subject, String msg) {
        host = "smtp.mail.yahoo.com";
        port = "465";

        emailid = "fromemailid";
        username = "fromusername";
        password = "******";

        props.put("mail.smtp.host", host);
        props.put("mail.smtp.auth", "true");
        //props.put("mail.debug", "true");
        props.put("mail.smtp.port", port);
        props.put("mail.smtp.starttls.enable","true");
        props.put("mail.smtp.socketFactory.port", port);
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");
        //props.put("mail.smtp.from", "bounceemailid"); // not working
        l_session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });
        //l_session.setDebug(true); // Enable the debug mode

        try {
            MimeMessage message = new MimeMessage(l_session);

            //message.addFrom(InternetAddress.parse("bounceemail@yahoo.com")); // not working
            //message.setReplyTo(InternetAddress.parse("bounceemail@yahoo.com")); // not working
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
            message.setSubject(subject);
            message.setContent(msg, "text/html");
            Transport.send(message);
            System.out.println("Message Sent");
        } catch (MessagingException mex) {
            mex.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static public void main(String args[]){
        bounceMail b = new bounceMail();
        b.sendMessage("junkemailid","Test","test Mail");
    }

}

用于阅读邮件的代码

import java.util.Properties;

import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.search.FlagTerm;



public class CheckingMail {

  public static void check(String host, String storeType, String user,
  String password) 
  {
    try {

    //create properties field
    Properties properties = new Properties();
    properties.put("mail.imap.host", host);
    properties.put("mail.imap.port", "993");
    properties.put("mail.imap.starttls.enable", "true");
    Session emailSession = Session.getDefaultInstance(properties);


    Store store = emailSession.getStore("imaps");
    store.connect(host, user, password);

    //create the folder object and open it
    Folder emailFolder = store.getFolder("INBOX");
    emailFolder.open(Folder.READ_ONLY);

    // retrieve the messages from the folder in an array and print it
    Message[] messages = emailFolder.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false)); 
    System.out.println("messages.length---" + messages.length);
    int bcnt = 0;
    for (int i = 0, n = messages.length; i < n; i++) {
       Message message = messages[i];
       if(message.getFrom()[0].toString().contains("MAILER-DAEMON")){
         bcnt++;
       }
       System.out.println("---------------------------------");
       System.out.println("Email Number " + (i + 1));
       System.out.println("Subject: " + message.getSubject());
       System.out.println("From: " + message.getFrom()[0]);

       Object content = message.getContent();
       if (content instanceof String)
           System.out.println("String");        

       if (content instanceof Multipart) {
           Multipart multipart = (Multipart) content;
           for (int j = 0; j < multipart.getCount(); j++) {
              System.out.println(multipart.getBodyPart(j).getFileName());
           }
       }

       for(int j=0;j<message.getAllRecipients().length; j++){
           System.out.println("Recipients "+j+" : " + message.getAllRecipients()[j]);
       }
       System.out.println("Date: " + message.getSentDate());
    }
    System.out.println("Bounce count : " + bcnt);

    //close the store and folder objects
    emailFolder.close(false);
    store.close();

    } catch (NoSuchProviderException e) {
       e.printStackTrace();
    } catch (MessagingException e) {
       e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static void main(String[] args) {
     String host = "imap.mail.yahoo.com";
     String mailStoreType = "imap";
     String username = "emailid";
     String password = "*****";
     check(host, mailStoreType, username, password);

  }

}

是否可以在发送邮件时设置自己的标题或属性,以便在邮件退回时读取?

【问题讨论】:

    标签: java email jakarta-mail


    【解决方案1】:

    你读过this JavaMail FAQ entry吗?

    您可以通过设置“mail.smtp.from”属性来设置用于返回失败消息的 SMTP“信封发件人”地址,如 javadocs for the com.sun.mail.smtp package 中所述。

    【讨论】:

    • 我试过了,使用时出现以下错误@Transport.send com.sun.mail.smtp.SMTPSendFailedException: 553 From address not verified - see http://help.yahoo.com/l/us/yahoo/mail/original/manage/sendfrom-07.html; at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2057) at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1580) at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1097) at javax.mail.Transport.send0(Transport.java:195) at javax.mail.Transport.send(Transport.java:124) at bounceMail.sendMessage(bounceMail.java:49)
    • 我希望代码可以与所有邮件提供商一起使用,而不仅仅是雅虎。我不明白您所说的“验证其他邮件地址”是什么意思。该链接带我添加帐户,如果您的意思相同,那么我尝试了它,它仍然不起作用。
    • 一般来说,电子邮件提供商会限制您可以将哪些电子邮件地址用作“发件人”地址,以防止您“欺骗”您的邮件来源。一些电子邮件提供商有办法让您证明您拥有一个电子邮件地址,然后将该地址用作您发送的消息中的“发件人”地址。如何做到这一点因提供商而异。如果您已经为 Yahoo Mail 这样做了,但它仍然不允许您将该地址用作“信封发件人”地址,那么您可能不走运。您需要联系 Yahoo Mail 支持来确定。
    【解决方案2】:

    回答第二个问题。我可以使用以下方法从退回的邮件中读取附件的名称(需要更好的方法)

    BufferedReader reader = new BufferedReader(new StringReader(message.getContent().toString()));
    while ((str = reader.readLine()) != null)  {
        index = str.indexOf("filename");
        if(index >-1){
            filename = str.substring(index+9); // 9 characters -> "filename=" 
            if (filename.length() > 0) System.out.println("Attachment  : " + filename);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-04
      • 1970-01-01
      • 1970-01-01
      • 2015-01-01
      • 2018-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多