2012-02-11 13 views
0

これらのコードでメールを送信しようとしましたが、エラーがあります。Javamail APIを使用して添付ファイルでメールを送信できません

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

public class SendEmail { 

    public static void main(String args[]) throws Exception { 
    String host = "localhost"; 
    String from = "[email protected]"; 
    String to = "[email protected]"; 

    Properties properties = System.getProperties(); 

    properties.setProperty("mail.smtp.host", host); 

    Session session = Session.getDefaultInstance(properties); 

    Message message = new MimeMessage(session); 
    message.setFrom(new InternetAddress(from)); 
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); 
    message.setSubject("JavaMail Attachment"); 

    BodyPart messageBodyPart = new MimeBodyPart(); 

    messageBodyPart.setText("hi"); 

    Multipart multipart = new MimeMultipart(); 
    multipart.addBodyPart(messageBodyPart); 

    //attachment 
    messageBodyPart = new MimeBodyPart(); 
    String filename = "C:/Users/ME/Desktop/file.txt"; 
    DataSource source = new FileDataSource(filename); 
    messageBodyPart.setDataHandler(new DataHandler(source)); 
    messageBodyPart.setFileName(filename); 
    multipart.addBodyPart(messageBodyPart); 

    message.setContent(multipart); 

    Transport.send(message); 
    System.out.println("Msg Send ...."); 
    } 
} 

私は「HTTPステータス404」エラーを得たと説明がある「)要求されたリソースは(利用できません。」
なぜ、どのようにこれを解決するのか分かりますか?
私はプログラミングとJavaの初心者として申し訳ありません

ありがとうございます!

+0

スタックトレースまたはエラーが発生した行を指定してください。 – Thor

+0

エラーが発生した行は決して指定しません。これは、私の質問で言及した説明とHTTPステータス404を示すページにのみ向いています。 – Lloydworth

+0

何もログしていませんか? 「例外処理」という言葉を聞いたことがありませんか? – MozenRath

答えて

0

あなたはウェブサーバーの内部でこれをしようとしているようですね。

あなたは何とか自分のコードを呼び出す方法をWebサーバに伝える必要があります。これを行う最も簡単な方法は、代わりにJSPページ内でこれを行う可能性が最も高い方法です。

関連する問題