2012-05-07 8 views
0

私はjavamailでメール送信メソッドを書いています。 なぜ私は取得し、エラーとして理解できません:受信者が設定されていません。 これは私のコードです:私は、デバッグモードでのコメントで書いたように私はそれが私はもう受信者を見つけることができない第二の方法にのInputStreamに変換whantJavaメールgetInputStream受取人

public static void sendMail(String to, String subj, String body, String attachmentName, byte[] attachment, String mime) throws Exception { 
    Properties p = System.getProperties(); 
    Session session = Session.getInstance(p); 
    MimeMessage dummyMessage = new MimeMessage(session); 
    dummyMessage.setFrom(new InternetAddress(LovProvider.getOpzioni().get("mail.address"))); 
    dummyMessage.setSubject(subj); 
    String[] tos = to.split(";"); 
    Address[] tosAddr = new InternetAddress[tos.length]; 
    for (int i = 0; i < tos.length; i++) { 
     tosAddr[i] = new InternetAddress(tos[i]); 
    } 
    dummyMessage.setRecipients(Message.RecipientType.TO, tosAddr); 
    Multipart mp = new MimeMultipart(); 
    MimeBodyPart bp = new MimeBodyPart(); 
    bp.setText(body); 
    mp.addBodyPart(bp); 
    if (attachmentName != null && attachment != null) { 
     DataSource dataSource = new ByteArrayDataSource(attachment, mime); 
     MimeBodyPart attachBodyPart = new MimeBodyPart(); 
     attachBodyPart.setDataHandler(new DataHandler(dataSource)); 
      attachBodyPart.setFileName(attachmentName); 
      mp.addBodyPart(attachBodyPart); 
     } 
     dummyMessage.setContent(mp); 
     //***** DEBUGGING here I find the recipient 
     sendMail(dummyMessage.getInputStream()); 
    } 

    public static void sendMail(InputStream emlFile) throws Exception { 
     Properties props = System.getProperties(); 
     props.put("mail.host", LovProvider.getOpzioni().get("mail.out.host")); 
     props.put("mail.transport.protocol", LovProvider.getOpzioni().get("mail.out.protocol")); 
     props.put("mail." + LovProvider.getOpzioni().get("mail.out.protocol") + ".port", LovProvider.getOpzioni().get("mail.out.port")); 
     Session mailSession = Session.getDefaultInstance(props, PasswordAuthentication.getAuth(LovProvider.getOpzioni().get("mail.out.user"), LovProvider.getOpzioni().get("mail.out.password"))); 
     MimeMessage message = new MimeMessage(mailSession, emlFile); 
     //***** DEBUGGING here I CAN NOT find the recipient 
     Transport.send(message); 
    } 

iは、正しく最初の部分に設定された受信者を見ることができます。

+0

私は私がのMimeMessageのwriteToを使用する必要があり、見つかりました新しいInputStreamを作成するよりも、outputstream :-) – Tobia

答えて

0

ことは、私はあなたのコードをデバッグすることはできませんが、多分この例ではあなたを助けることができる: 例を経由してメールを送信/受信について/ Gmailから

http://famulatus.com/component/search/?searchword=gmail&searchphrase=all&Itemid=9999

+0

ありがとう、私は解決策を見つけた。私はgetInputStreamの代わりにwriteTo MimeMessageメソッドを使用しなければなりませんでした。 – Tobia

関連する問題