我正在尝试使用java mail api创建一个应用程序。我想从雅虎和Gmail帐户发送邮件。我使用的代码可以从yahoo和gmail帐户发送电子邮件。但问题是....当程序第一次运行时,如果我从gmail帐户发送邮件,那么即使我第二次进入雅虎帐户,邮件也是从gmail帐户发送的。!!!我不明白该怎么做。请帮忙......
MainActivity.java
.......
.......
if(sender.contains("gmail")){
try {
GmailSender ss = new GmailSender(sender, password,1);
ss.sendGMail(sub,msg,sender,str);
} catch (Exception e) {
//Log.e("SendMail", e.getMessage(), e);
}
}
else if(sender.contains("yahoo")){
try {
GmailSender ss = new GmailSender(sender, password,2);
ss.sendGMail(sub,msg,sender,str);
} catch (Exception e) {
//Log.e("SendMail", e.getMessage(), e);
}
}
GmailSender.java
public class GmailSender extends Authenticator {
String mailhost;// = "smtp.gmail.com";
private String user;
private String password;
Session sessionG;
static {
Security.addProvider(new com.receme.sentmailbackground.JSSEProvider());
}
public GmailSender(String user, String password,int flag) {
this.user = user;
this.password = password;
if(flag==1)
mailhost = "smtp.gmail.com";
else if(flag==2)
mailhost = "smtp.mail.yahoo.com";
Properties props = new Properties();
props.clear();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", mailhost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");
sessionG = Session.getDefaultInstance(props, this);
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
**This part is edited**
public synchronized void sendMail(String subject, String body, String sender, String recipients, String attachment) throws Exception {
try{
MimeMessage message = new MimeMessage(sessionG);
//DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
message.setSender(new InternetAddress(sender));
message.setSubject(subject);
//message.setDataHandler(handler)
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
BodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.setText(body);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart1);
//for attaching file from sdcard
MimeBodyPart messageBodyPart2 = new MimeBodyPart();
String filename = attachment;
if(!(filename.equalsIgnoreCase("no file is attached"))){
DataSource source = new FileDataSource(filename);
messageBodyPart2.setDataHandler(new DataHandler(source));
StringTokenizer st = new StringTokenizer(filename,"/");
String tempString = null;
while(st.hasMoreTokens()){
tempString = st.nextToken();
}
System.out.println(tempString);
//filename = tempString;
messageBodyPart2.setFileName(tempString);
multipart.addBodyPart(messageBodyPart2);
}
//6) set the multiplart object to the message object
message.setContent(multipart);
//7) send message
Transport.send(message);
System.out.println("success");
}catch(Exception e){
//e.printStackTrace();
System.out.println("Attached File is not found");
//Make a notification
}
}
// * ** * ** * ** * ** * //
public class ByteArrayDataSource implements DataSource {
private byte[] data;
private String type;
public ByteArrayDataSource(byte[] data, String type) {
super();
this.data = data;
this.type = type;
}
public ByteArrayDataSource(byte[] data) {
super();
this.data = data;
}
public void setType(String type) {
this.type = type;
}
public String getContentType() {
if (type == null)
return "application/octet-stream";
else
return type;
}
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(data);
}
public String getName() {
return "ByteArrayDataSource";
}
public OutputStream getOutputStream() throws IOException {
throw new IOException("Not Supported");
}
}}
JSSEProvider.java
public final class JSSEProvider extends Provider {
public JSSEProvider() {
super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");
AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() {
public Void run() {
put("SSLContext.TLS",
"org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
put("Alg.Alias.SSLContext.TLSv1", "TLS");
put("KeyManagerFactory.X509",
"org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
put("TrustManagerFactory.X509",
"org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");
return null;
}
});
}}