運が悪かったので、インターネット上の多くのエントリを見ました。
これが私のコードです:
import Java.io.IOException;
import Java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
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;
public class MailTest
{
public static void main(String[] args) throws AddressException, MessagingException, IOException
{
String Host = "***";
String from = "b";
String to = "***";
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.Host", Host);
// Get session
Session session = Session.getDefaultInstance(props, null);
// Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Hello JavaMail");
message.setText("Welcome to JavaMail");
// Handle attachment 1
MimeBodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.attachFile("c:/Temp/a.txt");
// Handle attachment 2
MimeBodyPart messageBodyPart2 = new MimeBodyPart();
messageBodyPart2.attachFile("c:/Temp/b.txt");
MimeMultipart multipart = new MimeMultipart("related");
multipart.addBodyPart(messageBodyPart1);
multipart.addBodyPart(messageBodyPart2);
message.setContent(multipart);
// Send message
Transport.send(message);
}
}
その結果、最初のファイルのみが添付されます。
プレーンメッセージを確認しましたが、同じ識別子を持つ他のファイルが含まれていますが、何らかの理由で添付ファイルにリストされていません。
助けていただければ幸いです、デイブ
Multipart multipart = new MimeMultipart("mixed");
for (String str : attachment_PathList) {
MimeBodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(str);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(source.getName());
multipart.addBodyPart(messageBodyPart);
}
msg.setContent(multipart);
Transport.send(msg);
try
{
String Host = "smtp.gmail.com";
String from = "sender gmail id";
String pass = "sender password";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true"); // added this line
props.put("mail.smtp.Host", Host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
String[] to = {"receiver ids"}; // In this array you can add more ids
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[to.length];
// To get the array of addresses
for( int i=0; i < to.length; i++ )
{ // changed from a while loop
toAddress[i] = new InternetAddress(to[i]);
}
System.out.println(Message.RecipientType.TO);
for( int i=0; i < toAddress.length; i++)
{
// changed from a while loop
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject("sending in a group");
message.setText("Welcome to JavaMail");
// set ur text message
BodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.setText("This is message body");
//file path that you want to attatch
// you are able to send multipule file simultaneously
String filename[] ={"C:\\Documents and Settings\\admin\\Desktop\\imp data\\emil id.txt" ,"C:\\Documents and Settings\\admin\\Desktop\\imp data\\emil id.txt"};//change accordingly
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart1);
for(int i=0 ; i<filename.length ; i++)
{
MimeBodyPart messageBodyPart2 = new MimeBodyPart();
DataSource source = new FileDataSource(filename[i]);
messageBodyPart2.setDataHandler(new DataHandler(source));
messageBodyPart2.setFileName(filename[i]);
multipart.addBodyPart(messageBodyPart2);
}
// both part add into maulti part
// set message content
message.setContent(multipart);
// Trans port the message
Transport transport = session.getTransport("smtp");
transport.connect(Host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}