我的应用程序必须发送一个文本文件,它首先必须生成一个String.该文本包含非ASCII符号,所以我希望它是UTF-8.我已经尝试了很多变种,但我收到的所有附件都是一些问号.并且,当我发送与邮件正文相同的文本时,它可以正常工作.
以下是生成带附件的MimeBodyPart的代码行:
String attachment = "??????";
messageBodyPart.setContent(new String(attachment.getBytes("UTF-8"),
"UTF-8"),"text/plain; charset=UTF-8");
Run Code Online (Sandbox Code Playgroud)
我也尝试使用没有任何转换的字符串,只使用字节,现在,如你所见,我试图从字节生成一个字符串...
我究竟做错了什么?(我确实记得在另一个项目中这样做,但是我不再能够访问它的源代码).
先感谢您.提莫菲.
UPDATE
阅读完回复后,经过一些不成功的试验后,我认为最好发布我的邮件代码.我有一个Mailer
类,它负责邮件发送,其他类可以调用它的静态sendMessage()
方法来发送消息.这一切都在Google App Engine上运行.
public static void sendMessage(String to, String subject, String msgBody,
String attachment) throws AddressException, MessagingException {
Properties props = new Properties();
Session mailSession = Session.getDefaultInstance(props, null);
Message msg = new MimeMessage(mailSession);
String email = "bla-bla-bla"; // userService.getCurrentUser().getEmail();
msg.setFrom(new InternetAddress(email));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
InternetAddress[] addresses = { new InternetAddress("bla-bla-bla") };
msg.setReplyTo(addresses);
msg.setSubject(subject);
Calendar cal = Calendar.getInstance();
String fileName = cal.get(Calendar.YEAR) + …
Run Code Online (Sandbox Code Playgroud) JavaMail指定了一组可以设置为配置SMTP连接的属性.要使用STARTTLS,必须设置以下属性
mail.smtp.starttls.enable=true
Run Code Online (Sandbox Code Playgroud)
在哪里指定用户名/密码才能使用smtp服务?是否足以指定:
mail.smtp.user=me
mail.smtp.password=secret
Run Code Online (Sandbox Code Playgroud)
或者我必须使用以下方法明确登录:
transport.connect(server, userName, password)
Run Code Online (Sandbox Code Playgroud)
是的,我已经尝试过这样做,似乎有必要使用transport.connect(..)进行连接.但如果是,mail.smtp.user&pass属性是什么?他们还不足以使用smtp和starttls吗?
当我使用imaps连接到我的imap服务器时,它很脆弱.
你能告诉我如何忽略javamail中的服务器证书错误
Exception in thread "main"
javax.mail.MessagingException:
sun.security.validator.ValidatorException:
PKIX path building failed:
sun.security.provider.certpath.SunCertPathBuilderException:
unable to find valid certification
path to requested target; nested
exception is:
javax.net.ssl.SSLHandshakeException:
sun.security.validator.ValidatorException:
PKIX path building failed:
sun.security.provider.certpath.SunCertPathBuilderException:
unable to find valid certification
path to requested target at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:665)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)
at App20110204.main(App20110204.java:31)
Caused by:
javax.net.ssl.SSLHandshakeException:
sun.security.validator.ValidatorException:
PKIX path building failed:
sun.security.provider.certpath.SunCertPathBuilderException:
unable to find valid certification
path to requested target at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1623)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:198)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:192)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1074)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:128)
at …
Run Code Online (Sandbox Code Playgroud) 我无法让应用程序使用JavaMail API以比以前更自动化的方式发送一些文件.我是Java和NetBeans的新手,但是已经用其他语言编程了,所以如果我对Java和NetBeans有点遗失,请原谅我.
我一直收到这个错误
java.net.SocketException:权限被拒绝:连接
尝试连接到本地邮件服务器时.我已经通过gmail的SMTP服务器使用相同的代码成功连接和发送邮件,只需更改用户名,密码和端口.我也能成功telnet到我们的服务器并从端口25获得220响应.我还有一个运行的批处理文件,它通过我们的本地服务器成功发送电子邮件.关于为什么我无法连接的任何想法或想法JavaMail
?
这是发送电子邮件的代码.
源代码:
public void sendEmail(String customerNumber, ArrayList fileList){
String from = "xxxx";
String username = "xxxx";
String to = "xxxx";
String host = "10.1.1.6";
String pwd = "xxxx";
String port = "25";
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
props.put("mail.smtp.user", username);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.socketFactory.port", port);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
Session session = Session.getInstance(props, null);
session.setDebug(true);
MimeMessage message = new MimeMessage(session);
try{
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, to);
message.setSubject("Electronic Invoices");
BodyPart …
Run Code Online (Sandbox Code Playgroud) 我正在使用javamail将电子邮件发送到收件人列表,但不希望他们能够看到其他人收到了电子邮件.我也不想使用BCC发送它,因为用户甚至没有在TO列表中看到自己.我认为这段代码会这样做,但它会显示TO列表中的所有收件人.除了创建循环并一次发送一封电子邮件之外,还有另一种方法吗?
(注意:recipients []是一个包含电子邮件地址的字符串数组.)
javax.mail.internet.InternetAddress[] addressTo = new javax.mail.internet.InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++)
{
addressTo[i] = new javax.mail.internet.InternetAddress(recipients[i]);
}
msg.setRecipients(javax.mail.Message.RecipientType.TO, addressTo);
Run Code Online (Sandbox Code Playgroud) 来自http://www.oracle.com/technetwork/java/faq-135477.html#sendmpa:
您将要发送MIME多部分/替代消息.使用使用新MimeMultipart("alternative")构造的MimeMultipart对象,构造此类消息的方式与构造multipart/mixed消息的方式基本相同.然后,将text/plain body部分作为multpart中的第一部分插入,并将text/html body部分作为multipart中的第二部分插入.您需要自己构建plain和html部分以获得适当的内容.有关此类消息的结构的详细信息,请参阅RFC2046.
有人可以给我看一些示例代码吗?
ClassFormatError
使用javaMail api在我的spring mvc web app上发送电子邮件时,我感到很奇怪.
下面是我的mail-cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<bean id="mailSource" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.host}" />
<property name="port" value="${mail.port}" />
<property name="username" value="${mail.username}" />
<property name="password" value="${mail.password}" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
</bean>
<bean id="mailHelper" class="com.jr.freedom.util.MailHelper">
<property name="mailSender" ref="mailSource"></property>
<property name="messageText"
value="Thank you for reigistering to the Freedom Application. Please copy and paste your activation code below \n\n"></property>
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
我的POM文件
<project …
Run Code Online (Sandbox Code Playgroud) 我正在用Java创建一个应用程序,通过Java从我的GoDaddy webmail中读取邮件.我已经使用了javax.mail.当我调试程序时,它会在行中出错:bodyPart.isMimeType("text/plain")
出乎意料的是,它以正确的方式处理第一封邮件,但却为第二封邮件提供了错误.
这是完整的堆栈跟踪:
javax.mail.MessagingException: Unable to load BODYSTRUCTURE
at com.sun.mail.imap.IMAPMessage.loadBODYSTRUCTURE(IMAPMessage.java:1306)
at com.sun.mail.imap.IMAPMessage.getContentType(IMAPMessage.java:465)
at javax.mail.internet.MimeBodyPart.isMimeType(MimeBodyPart.java:1050)
at javax.mail.internet.MimeMessage.isMimeType(MimeMessage.java:986)
at asanaLambdaIntegrationDebugMode.getNameMailIdFromMessage(asanaLambdaIntegrationDebugMode.java:1318)
at asanaLambdaIntegrationDebugMode.main(asanaLambdaIntegrationDebugMode.java:978)
Run Code Online (Sandbox Code Playgroud)
对此有何建议?
我正在尝试使用spring boot发送电子邮件,但我得到:
java.lang.UnsupportedOperationException: Method not yet implemented
at javax.mail.internet.MimeMessage.<init>(MimeMessage.java:89)
at org.springframework.mail.javamail.SmartMimeMessage.<init>(SmartMimeMessage.java:52)
at org.springframework.mail.javamail.JavaMailSenderImpl.createMimeMessage(JavaMailSenderImpl.java:325)
Run Code Online (Sandbox Code Playgroud)
我使用过这个maven条目:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.6.RELEASE</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>1.2.6.RELEASE</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
application.properties:
spring.mail.host=smtp.gmail.com
spring.mail.port= 25
spring.mail.username= test
spring.mail.password= test
Run Code Online (Sandbox Code Playgroud)
我的代码:
@Autowired
private JavaMailSender javaMailSender;
private void send() {
MimeMessage mail = javaMailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(mail, true);
helper.setTo("mymail@mail.co.uk");
helper.setReplyTo("someone@localhost");
helper.setFrom("someone@localhost");
helper.setSubject("Lorem ipsum");
helper.setText("Lorem ipsum dolor sit amet [...]");
} catch (MessagingException e) {
e.printStackTrace();
} finally {}
javaMailSender.send(mail);
//return …
Run Code Online (Sandbox Code Playgroud) 我想发送一封电子邮件到非ASCII电子邮件地址,我不确定使用JDK8的推荐程序是什么.
我该如何处理以下电子邮件地址?
是否有任何安全注意事项需要注意?
这个示例代码是否足够?
import java.net.IDN;
public class IDNMailHelper {
public static String toIdnAddress(String mail) {
if (mail == null) {
return null;
}
int idx = mail.indexOf('@');
if (idx < 0) {
return mail;
}
return localPart(mail, idx) + "@" + IDN.toASCII(domain(mail, idx));
}
private static String localPart(String mail, int idx) {
return mail.substring(0, idx);
}
private static String domain(String mail, int idx) {
return mail.substring(idx + 1);
}
}
Run Code Online (Sandbox Code Playgroud) jakarta-mail ×10
java ×10
email ×3
spring ×2
attachment ×1
encoding ×1
java-8 ×1
javabeans ×1
smtp ×1
spring-boot ×1
ssl ×1
starttls ×1