我在使用Android应用程序上的Retrofit2尝试通过MailGun发送电子邮件时遇到了实际问题。下面的代码没有给出任何错误,但也没有用,我不知道MailGun或Retrofit2如何工作,以便知道从哪里开始对下面的代码进行故障排除,并且我很难在网上找到帮助。< / p>
这是我的MailHandler类。
public class MailHandler {
private static final String TAG = "MailHandler";
public static final String ENDPOINT = "https://api.mailgun.net/v3/<domain redacted>/messages/";
public static final String ACCEPT_JSON_HEADER = "Accept: application/json";
public static final String BASIC = "Basic";
String apiKey = "api:key-<redacted>";
String fromText = "Boaty McBoatface<noreply@boatymcboatface.co.uk>";
private MailInterface api;
public interface MailInterface {
@Headers({ACCEPT_JSON_HEADER})
@FormUrlEncoded
@POST("/messages")
Call<Void> authUser (@Header("Authorization") String authorizationHeader,
@Field("from") String from,
@Field("to") String to,
@Field("subject") String subject,
@Field("text") String text);
}
public MailHandler() {
Retrofit retrofit = getAuthAdapter();
api = retrofit.create(MailInterface.class);
}
public Retrofit getAuthAdapter() {
return new Retrofit.Builder().baseUrl(ENDPOINT).addConverterFactory(GsonConverterFactory.create(new Gson())).build();
}
public void newEmail(String email) {
String authHeader = BASIC + " " + Base64.encodeToString(apiKey.getBytes(), Base64.NO_WRAP);
String subj = "Test Subject";
String body = "Test Email";
api.authUser(authHeader, fromText, email, subj, body);
}
}
答案 0 :(得分:0)
响应很晚,但是这里的问题是您遇到了api端点。 (以防其他人)
https://api.mailgun.net/v3/<domain redacted>/messages/messages/
不存在。您的基本网址应该仅为https://api.mailgun.net/v3/<domain redacted>/
,因为当您调用authUser
方法时,改进会添加到该网址中。