使用JDK1.5如何使用JavaMail API轻松发送二进制attachemnt(如PDF文件)?
这是我在下面的代码来自java教程 - 但是当我尝试接收从计算机发送的正常消息时,我的问题就出现了,而不是通过GMail发送.如果我通过GMail接收电子邮件它运行正常并返回邮件,但是尝试从传统桌面邮件客户端检索邮件返回
错误:
java.lang.ClassCastException: java.lang.String cannot be cast to javax.mail.Multipart
at gridnotifierproject_pcbuild.HandleMailInput.retrieveOneMail(HandleMailInput.java:37)
at gridnotifierproject_pcbuild.GridNotifierProject_PCBuild.main(GridNotifierProject_PCBuild.java:22)
Run Code Online (Sandbox Code Playgroud)
码:
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imaps");
try {
Session session = Session.getInstance(props, null);
Store store = session.getStore();
store.connect("imap.gmail.com", "***********@gmail.com", "******");
System.out.println("Established Connection to Server!");
Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_ONLY);
Message msg = inbox.getMessage(inbox.getMessageCount());
System.out.println("Found specified Folder, retrieving the latest message...");
Address[] in = msg.getFrom();
for (Address address : in) {
System.out.println("FROM:" + address.toString());
}
Multipart mp = (Multipart) msg.getContent();
BodyPart bp = mp.getBodyPart(0); …Run Code Online (Sandbox Code Playgroud) 我正在使用 javax.mail 发送一些电子邮件。它工作正常,只是它不会将消息发送到 CC/BCC
message.setRecipient(Message.RecipientType.TO, new InternetAddress("a@x.com"));
message.setRecipient(Message.RecipientType.CC, new InternetAddress("b@x.com"));
Run Code Online (Sandbox Code Playgroud)
a@x.com 正确收到消息(其中还标明了抄送地址),但 b@x.com 没有收到
这里是完整的代码:
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", "192.168.1.1");
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
// Crea un authenticator (per mandare mail anche all'esterno del dominio)
Authenticator authenticator = new Interfaccia_CSR.SMTPAuthenticator();
// Get session
Session session = Session.getInstance(props, authenticator);
// Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("sender@x.com"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("a@x.com"));
message.setRecipient(Message.RecipientType.CC, new InternetAddress("b@x.com"));
message.setSubject(oggetto);
// create the message part
MimeBodyPart …Run Code Online (Sandbox Code Playgroud) 我从MimeMessage获得输入流.在InputStream中,最后我要添加\ r \n.\ r \n
表示消息的结尾.
请建议.
我目前有一个邮件服务器正在运行。我可以通过 mywebsite.com/roundcube/ 登录我的网络邮件客户端
网络邮件允许我适当地发送和接收电子邮件。日志显示所有进出的邮件都很好。我已经从这里发送到我的 gmail 帐户并返回。
但是,我真正想做的是使用 javax 邮件从 java 发送一些邮件。所以我可以自动化电子邮件
我试过如下配置:
final String username = "myusername";
final String password = "mypassword";
Properties props = new Properties();
props.put("mail.smtp.host", "localhost");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "25");
props.put("mail.debug","true");
// Get the Session object.
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
session.setDebug(true);
Run Code Online (Sandbox Code Playgroud)
当我去发送消息时
Transport.send(message);
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
DEBUG: JavaMail version 1.5.0-b01
DEBUG: setDebug: JavaMail version 1.5.0-b01
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG …Run Code Online (Sandbox Code Playgroud) 我有以下代码可以使用 Java 阅读 Gmail 电子邮件。如果电子邮件包含非常简单的纯文本消息,则代码运行良好,我可以看到(正文)的内容正确显示。
但是,对于某些电子邮件,我在尝试时看到以下消息
显示正文(内容:javax.mail.internet.MimeMultipart@2038ae61)
. 我发现很难解决这个问题。请帮我。
代码 :
import java.util.Properties;
import javax.activation.DataHandler;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeMultipart;
public class Experiment {
public static void main(String[] args) {
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imaps");
try {
Session session = Session.getInstance(props, null);
Store store = session.getStore();
store.connect("imap.gmail.com", "username@gmail.com",
"password");
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
Message msg = inbox.getMessage(inbox.getMessageCount());
Address[] in = msg.getFrom();
for (Address address : in) …Run Code Online (Sandbox Code Playgroud) 我正在使用Java编写Android应用程序,部分应用程序会将电子邮件帐户中的电子邮件发送到另一个电子邮件帐户.我正在使用JavaMail来实现这一目标.
这一切都运行得很好,除了我发送的电子邮件帐户的密码硬编码到我的程序中的变量.我不希望用户能够访问此变量.我需要加密吗?我该怎么做呢?有没有办法配置JavaMail所以我不必将我的密码存储在设备的任何地方?
这是我用来存储电子邮件和密码的代码.它只是自己类中的静态变量:
public class Config {
public static final String EMAIL ="[email]@gmail.com";
public static final String PASSWORD ="[password]";
}
Run Code Online (Sandbox Code Playgroud)
我尝试在帐户上激活双因素身份验证,但在这种情况下JavaMail失败了.
我不想进入应用程序的细节,但我家乡以外的任何人都没有任何理由下载此应用程序.这完全是为了本地目的.这是我从未在其他任何地方使用的密码,它是我专门为此应用程序制作的电子邮件帐户,因此我不会在此处承担重大风险.我仍然希望尽可能安全.
我想将电子邮件从我的Java应用程序发送到gmail帐户,我使用了以下代码和javaMail API,但是Gmail不接受用户名,密码和引发的异常有人可以帮助我解决此问题吗?
MailService.java
public class MailService {
String email;
String content;
public void sendMail(String email,String content)
{
this.email=email;
this.content=content;
// Recipient's email ID needs to be mentioned.
String to = email;
// Sender's email ID needs to be mentioned
String from = senderemail@gmail.com;
final String username = "myusername";//change accordingly
final String password = "*******";//change accordingly
// Assuming you are sending email through relay.jangosmtp.net
String host = "smtp.gmail.com";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587"); …Run Code Online (Sandbox Code Playgroud) 我尝试使用 来阅读一些电子邮件javamail lib。当电子邮件包含 MIME 标头时(Content-Type: text/plain; charset="unknown-8bit"),我收到此错误:java.io.UnsupportedEncodingException: unknown-8bit
有什么想法为什么会发生这种情况吗?
我的java邮件配置文件是
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"
p:host="${mail.sender.host}" p:port="${mail.sender.port}" p:username="${mail.sender.username}"
p:password="${mail.sender.password}" p:protocol="${mail.sender.protocol}"
p:javaMailProperties-ref="mailProperties" />
<bean id="emailClient"
class="com.covisint.validationAdmin.validationAdminBackend.smtpMail.EmailClient"
p:mailSender-ref="mailSender" p:emailSubject="${email.subject}" />
<util:properties id="mailProperties">
<prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
<prop key="mail.smtp.starttls.enable">${mail.smtp.starttls.enable}</prop>
<prop key="mail.smtp.quitwait">${mail.smtp.quitwait}</prop>
<prop key="mail.smtp.debug">${mail.smtp.debug}</prop>
<prop key="mail.smtp.socketFactory.port">${mail.smtp.socketFactory.port}</prop>
<prop key="mail.smtp.socketFactory.class">${mail.smtp.socketFactory.class}</prop>
<prop key="mail.smtp.socketFactory.fallback">${mail.smtp.socketFactory.fallback}</prop>
</util:properties>
Run Code Online (Sandbox Code Playgroud)
我的属性文件是
email.subject = my email subject
mail.sender.host =smtp.gmail.com
mail.sender.port =465 //465 or 587 (both dint work)
mail.sender.username =****@gmail.com
mail.sender.password =******
mail.sender.protocol =smtp
mail.smtp.auth =true
mail.smtp.starttls.enable =true
mail.smtp.quitwait =false
mail.smtp.debug=true
mail.smtp.socketFactory.port=465
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.socketFactory.fallback=false
Run Code Online (Sandbox Code Playgroud)
我收到java邮件的跟随错误
org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Unknown SMTP host: smtp.gmail.com; …Run Code Online (Sandbox Code Playgroud)