简体   繁体   English

使用数据库以Java发送电子邮件

[英]Sending email in Java by using database

I am currently doing a project where I am required to send an email to the specific address taken from database. 我目前正在做一个项目,要求我发送电子邮件到从数据库获取的特定地址。 However, the column "email" in database does not actually contain emails, but names instead. 但是,数据库中的“电子邮件”列实际上并不包含电子邮件,而是名称。 So in database there are full names in russian language like 因此在数据库中有俄语全名,例如

"Иванов Александр" which is "Ivanov Alexandr". “ИвановАлександр”是“伊万诺夫·亚历山大”。 So when I type this name in the outlook it automatically finds his email: AIvanov@domainname.com. 因此,当我在Outlook中键入此名称时,它将自动找到他的电子邮件:AIvanov@domainname.com。 But in my java code when I use name "Иванов Александр" i keep getting error. 但是在我的Java代码中,当我使用名称“ИвановАлександр”时,我总是收到错误消息。

Here is my java code 这是我的java代码

File[] listOfFiles = outDir.listFiles();
    if (outDir.isDirectory()) {
        if (outDir.list().length > 0) {
            for (File file : listOfFiles) {
                    Session session_m = Session.getDefaultInstance(props, null);
                    MimeMessage message = new MimeMessage(session_m);
                    message.setFrom (new InternetAddress("mmm@domainname.com", "mmm@domainname.com"));
                    InternetAddress i = new InternetAddress("\""+email+"\"", false);
                    message.addRecipient(Message.RecipientType.TO, i);
                    message.setSubject("test");
                    message.setText("test");
                    message.setHeader("Content-Type","text/plain;charset=windows-1251");

                    MimeBodyPart mbp1 = new MimeBodyPart();
                    FileDataSource fds = new FileDataSource(file);
                    mbp1.setDataHandler(new DataHandler(fds));
                    mbp1.setFileName(fds.getName());

                    Multipart mp = new MimeMultipart();
                    mp.addBodyPart(mbp1);

                    System.out.println("[EmailHandler] Attaching the following file to email: " + fds.getName());
                    message.setContent(mp);

                    SMTPTransport t = (SMTPTransport)session_m.getTransport("smtp");
                    t.connect("mail.domainname.com", "main@domainname.com", null);
                    System.out.println("[EmailHandler] Sending email... ");
                    t.sendMessage(message, message.getAllRecipients());
                    file.delete();
                    Thread.sleep(3000);
                }
            } else {
                System.out.println("[EmailHandler] Folder " + outDir.getName() + " is empty... nothing to attach");
            }
        } else {
            System.out.println("Folder not found... Check the path");
        }

In this code the String email is Иванов Александр. 在此代码中,字符串电子邮件为ИвановАлександр。

And I kept getting this error 而且我不断收到这个错误

javax.mail.internet.AddressException: Local address contains control or whitespace in string ``Иванов Александр'' javax.mail.internet.AddressException:本地地址包含字符串``ИвановАлександр''中的控件或空格

So would like to know the ways I can make this string go through. 因此想知道如何使此字符串通过。

Thank you. 谢谢。

The outlook uses its address book to map a name to one of the email. Outlook使用其通讯簿将名称映射到其中一封电子邮件。 That is why it is working fine, if you manually try creating a new email and just put the name. 这就是为什么它工作正常的原因,如果您手动尝试创建新电子邮件并仅输入名称。 Outlook simply do a lookup in the address book and find out the email address. Outlook只是在通讯簿中进行查找并找出电子邮件地址。

However, this is not the same with a java program. 但是,这与Java程序不同。 The program needs exact email address to send an email. 该程序需要准确的电子邮件地址才能发送电子邮件。 Now there could be many ways to find out email address. 现在可以有很多方法来查找电子邮件地址。

The simplest approach is to store the email address in one of the database table. 最简单的方法是将电子邮件地址存储在数据库表之一中。 If the person is associated with the company's SMTP system/active directory; 如果此人与公司的SMTP系统/活动目录关联; you could use java smtp API / active directory APIs to find out the email or alias (usually be the part of email id before @) and then create email id to be used into the program to send email. 您可以使用Java smtp API /活动目录API来查找电子邮件或别名(通常是@之前的电子邮件ID的一部分),然后创建要用于程序中的电子邮件ID。

You need to provide a valid mail address. 您需要提供一个有效的邮件地址。 If you are using a fixed structure for your mails just convert the name to latin characters and append @domain.com If you are not using any rules for your mail adress creation then I suggest you to add an email field to the database 如果您对邮件使用固定的结构,只需将名称转换为拉丁字符并附加@ domain.com。如果您不对创建邮件地址使用任何规则,那么建议您向数据库中添加一个电子邮件字段

Don't know if this resolves your question though 不知道这是否可以解决您的问题

Regards 问候

Your program have to 您的程序必须

  1. Validate e-mail. 验证电子邮件。 It can be implemented using regular expression: Using a regular expression to validate an email address 可以使用正则表达式来实现: 使用正则表达式来验证电子邮件地址

  2. Get email from contact list when field contains only name. 当字段仅包含姓名时,从联系人列表中获取电子邮件。 There are two library for working with outlook contacts: Open source java library to read outlook emails, calendar etc 有两个与Outlook联系人一起使用的库: 开源Java库,用于读取Outlook电子邮件,日历等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM