格式错误的电子邮件地址-InternetAddress

时间:2019-02-22 08:50:41

标签: java

我无法创建通过org.springframework.mail.javamail.MimeMessageHelper发送的电子邮件地址:

final InternetAddress from = new InternetAddress("foo@example.com", "Two words");

,接收到的消息的结果为From,格式为:

"Two words <foo@example.com>" <foo@example.com>

但是我想要的是这个

"Two words" <foo@example.com>

问:我的代码有什么问题?

1 个答案:

答案 0 :(得分:2)

使用javax.mail 1.5.0-b1版本和以下程序

GlobalConfiguration.Configuration
                .EnableSwagger(c =>
                {
 c.OperationFilter(() => new AddRequiredAuthorizationHeaderParameter());
 });

 public class AddRequiredAuthorizationHeaderParameter : IOperationFilter
    {
        public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
        {
            var filterPipeline = apiDescription.ActionDescriptor.GetFilterPipeline();
            var isAuthorized = filterPipeline.Select(filterInfo => filterInfo.Instance)
                                    .Any(filter => filter is IAuthorizationFilter);

            var allowAnonymous = apiDescription.ActionDescriptor.GetCustomAttributes<AllowAnonymousAttribute>().Any();

            if (operation.parameters == null)
                operation.parameters = new List<Parameter>();

            operation.parameters.Add(new Parameter
            {
                name = "Authorization",
                @in = "header",
                type = "",
                required = true,
                description = "User Name"
            });
        }
    }

输出

import java.io.UnsupportedEncodingException;

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

public class Email {

    public Email() {
    }

    public static void main(String[] args) throws UnsupportedEncodingException, AddressException {


        final InternetAddress from = new InternetAddress("foo@example.com", "Two words");

        System.out.println(from);
        final InternetAddress from2 = new InternetAddress("\"Two words\" <foo@example.com>");

        System.out.println(from2);
    }

}

...