我已经多次看到使用Android Intent Action.SEND在前台发送电子邮件的难易程度.
但是,我需要实施一个反馈表,通过电子邮件向我们发送完成后的详细信息.这并不适合Android Intent,因为用户不希望在发送之前看到生成的电子邮件.
如果我们假设smtp服务器是lol.does.thiswork并且用户名= lala,密码= po,那么是否有任何人可以使用后台发送电子邮件的示例代码.
干杯
我是Java Mail的新手.我首先想要执行程序(我通过我的老年人),看看是否一切正常.因此,当我编译该代码时,我发现错误,找不到所有Java邮件的类和包.
任何人都可以列出我的程序编译和执行所需的东西没有任何问题.我下载了"JAva Mail 1.4.5",但是没有安装文件?
我有JAva 1.6和Windows XP
请帮助........
错误:C:>javac SMTPClient.java
SMTPClient.java:2: package javax.mail does not exist
import javax.mail.;
^
SMTPClient.java:3: package javax.mail.internet does not exist
import javax.mail.internet.;
^
SMTPClient.java:18: cannot find symbol
symbol : class Session
location: class SMTPClient
Session session = Session.getDefaultInstance(properties);
^
SMTPClient.java:18: cannot find symbol
symbol : variable Session
location: class SMTPClient
Session session = Session.getDefaultInstance(properties);
^
SMTPClient.java:21: cannot find symbol
symbol : class MimeMessage
location: class SMTPClient
MimeMessage message = new …
使用以下代码从Zimbra邮件服务器读取邮件.
String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties pop3Props = new Properties();
pop3Props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
pop3Props.setProperty("mail.pop3.socketFactory.fallback", "false");
pop3Props.setProperty("mail.pop3.auth", "true");
pop3Props.setProperty("mail.pop3.port", "110");
pop3Props.setProperty("mail.pop3.socketFactory.port", "110");
URLName url = new URLName("pop3", host, 110, "",username, password);
Session session = Session.getInstance(pop3Props, null);
Store store = new POP3SSLStore(session, url);
store.connect();
Run Code Online (Sandbox Code Playgroud)
但不断收到此错误
javax.mail.MessagingException: Connect failed;
nested exception is:
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:148)
at javax.mail.Service.connect(Service.java:275)
at javax.mail.Service.connect(Service.java:156)
at javax.mail.Service.connect(Service.java:105)
at VerifyMail.readPOP3(VerifyMail.java:177)
at VerifyMail.main(VerifyMail.java:20)
Caused by: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
at com.sun.net.ssl.internal.ssl.InputRecord.handleUnknownRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.InputRecord.read(Unknown Source) …Run Code Online (Sandbox Code Playgroud) 我正在处理与外部IMAP服务器的错误连接.我需要我的应用程序定期从此邮件服务器下载邮件.在很多情况下,我的代码已经使用javamail下载了消息并执行了message.getContent,然后处理各个正文部分(附件).在执行此操作的过程中,连接可能会丢失.
因此,我需要确保消息在执行message.getContent时没有自动标记为SEEN,这就是Javamail的行为方式,如本回复中所述:https://stackoverflow.com/a/7679091/303106
有没有办法确保未下载的邮件未标记为SEEN?
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. a5sm29349940pbw.4 - gsmtp
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1829)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1368)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:886)
at javax.mail.Transport.send0(Transport.java:191)
at javax.mail.Transport.send(Transport.java:120)
at com.conceptbuild.EmailGenerator.main(EmailGenerator.java:120)
Run Code Online (Sandbox Code Playgroud)
package com.conceptbuild;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
public class EmailGenerator {
public EmailGenerator() {
// TODO Auto-generated constructor stub
}
private class MailAuthenticator extends Authenticator {
String username = "username";
String …Run Code Online (Sandbox Code Playgroud) 我能够正确地从字段和主题字段获取,但即使我使用toString()方法,我仍然以对象格式获取邮件内容.
请查看以下源代码
try {
Session session = Session.getInstance(props, null);
Store store = session.getStore();
store.connect("imap.gmail.com", "mymail@gmail.com", "****");
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
Message msg = inbox.getMessage(inbox.getMessageCount());
Address[] in = msg.getFrom();
for (Address address : in) {
System.out.println("FROM:" + address.toString());
}
Object obj = msg.getContent();
//Multipart mp = (Multipart)obj;
Multipart mp = (Multipart) msg.getContent();
// MimeBodyPart part = (MimeBodyPart)mp.getBodyPart(0);
BodyPart bp = ((Multipart) msg.getContent()).getBodyPart(0);
// Object body = msg.getContent();
//String value = String.valueOf(body);
System.out.println("SENT DATE:" + msg.getSentDate());
System.out.println("SUBJECT:" + msg.getSubject());
System.out.println("CONTENT:" + …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用JavaMail发送邮件,但获得IO异常,说访问被拒绝 请让我知道我在哪里犯了错误.我使用了java邮件和Internet API以及数据源,datahandler API.我试图作为附件发送的html文件也在适当的路径中,我拥有该文件的访问权限.以下是我的代码
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class SendMail
{
public static void execute(String reportFileName) throws Exception
{
String path="C:/reports";
String[] to={"xyz@gmail.com"};
String[] cc={"xyz@gmail.com"};
String[] bcc={"abc@gmail.com"};
SendMail.sendMail("xyz@gmail.com",
"******",
"smtp.gmail.com",
"465",
"true",
"true",
true,
"javax.net.ssl.SSLSocketFactory",
"false",
to,
cc,
bcc,
"Test Mail",
"Test Message",
path,
reportFileName);
}
public static boolean sendMail(String userName,
String passWord,
String host,
String port,
String starttls,
String auth,
boolean debug,
String socketFactoryClass,
String fallback,
String[] …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用基于imap协议的java mail api连接到我的邮箱.我检查并确定我插入了正确的参数.这是我得到的以下异常:
[警告]请通过您的网络浏览器登录:https://support.google.com/mail/accounts/answer/78754(失败)
我不知道为什么会发生这种情况我在我的Gmail帐户设置中启用了imap选项.
我是我的代码:
Properties protocol = new Properties();
protocol.setProperty("mail.store.protocol", "imaps");
try{
Session session = Session.getInstance(protocol, null);
Store store = session.getStore();
String host = prop.getProperty("host");
String email = prop.getProperty("username");
String password = prop.getProperty("password");
store.connect(host, email, password);
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
int messageCount = inbox.getMessageCount();
model.addAttribute("msg","number of mails"+" "+messageCount);
Message[] messages = inbox.getMessages();
PrintWriter writer = new PrintWriter(username+".txt", "UTF-8");
for(int i=0;i<messageCount || prop.getProperty("status").equals(status.RUNNING.toString()) ;i++ ){
model.addAttribute("msg","Reading Mails");
Multipart mp = (Multipart) messages[i].getContent();
BodyPart bp …Run Code Online (Sandbox Code Playgroud) 我将Message msg分成Multipart multi1 =(Multipart)msg.getContent()。邮件附件位于一个BodyPart中,Part part = multi1.getBodyPart(i); 然后我要保存附件。
private void saveFile(String fileName, InputStream in) throws IOException {
File file = new File(fileName);
if (!file.exists()) {
OutputStream out = null;
try {
out = new BufferedOutputStream(new FileOutputStream(file));
in = new BufferedInputStream(in);
byte[] buf = new byte[BUFFSIZE];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
} catch (FileNotFoundException e) {
LOG.error(e.toString());
} finally {
// close streams
if (in != null) { …Run Code Online (Sandbox Code Playgroud) 由于包含了以下测试,因此执行大约需要5秒钟m.saveChanges().
import org.junit.Before;
import org.junit.Test;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.util.Properties;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@Test
public void test1() throws MessagingException, IOException {
Session s = Session.getDefaultInstance(new Properties());
MimeMessage m = new MimeMessage(s);
m.setContent("<b>Hello</b>", "text/html; charset=utf-8");
m.saveChanges();
assertEquals(m.getContent(), "<b>Hello</b>");
assertEquals(m.getContentType(), "text/html; charset=utf-8");
}
Run Code Online (Sandbox Code Playgroud)
我也用mockito嘲笑了Session,但它没有帮助:
Session s = mock(Session.class);
when(s.getProperties()).thenReturn(new Properties());
Run Code Online (Sandbox Code Playgroud)
这里有什么问题?我可以嘲笑什么来加快速度?
jakarta-mail ×10
java ×6
email ×2
smtp ×2
android ×1
gmail ×1
imap ×1
io ×1
junit ×1
mime-message ×1
performance ×1
pop3 ×1
zimbra ×1