问题描述
在这里,我已经在标准.NET库上创建了我的项目,以获取/发布发票。但是我想通过电子邮件发送以该名称创建发票。这是我下面创建发票的示例代码。
textarea
现在,当我了解到Xero允许将电子邮件发送到指定的发票时,这是我了解的链接。
https://developer.xero.com/documentation/api/invoices#email
但是,当尝试在 .NET标准库中为Xero 查找方法时,我偶然发现了该方法。
public async Task<ActionResult> Create(string Name,string LineDescription,string LineQuantity,string LineUnitAmount,string LineAccountCode)
{
var xeroToken = TokenUtilities.GetStoredToken();
var utcTimeNow = DateTime.UtcNow;
var serviceProvider = new ServiceCollection().AddHttpClient().BuildServiceProvider();
var httpClientFactory = serviceProvider.GetService<IHttpClientFactory>();
XeroConfiguration XeroConfig = new XeroConfiguration
{
ClientId = ConfigurationManager.AppSettings["XeroClientId"],ClientSecret = ConfigurationManager.AppSettings["XeroClientSecret"],CallbackUri = new Uri(ConfigurationManager.AppSettings["XeroCallbackUri"]),Scope = ConfigurationManager.AppSettings["XeroScope"],State = ConfigurationManager.AppSettings["XeroState"]
};
if (utcTimeNow > xeroToken.ExpiresAtUtc)
{
var client = new XeroClient(XeroConfig,httpClientFactory);
xeroToken = (XeroOAuth2Token)await client.RefreshAccesstokenAsync(xeroToken);
TokenUtilities.Storetoken(xeroToken);
}
string accesstoken = xeroToken.Accesstoken;
string xeroTenantId = xeroToken.Tenants[0].TenantId.ToString();
//string xeroTenantId = xeroToken.Tenants[1].TenantId.ToString();
var contact = new Contact();
contact.Name = Name;
var line = new LineItem()
{
Description = LineDescription,Quantity = decimal.Parse(LineQuantity),UnitAmount = decimal.Parse(LineUnitAmount),AccountCode = LineAccountCode
};
var lines = new List<LineItem>() { line };
//var lines = new List<LineItem>();
//for (int j = 0;j < 5;j++)
//{
// lines.Add(line);
//}
var invoice = new Invoice()
{
Type = Invoice.TypeEnum.ACCREC,Contact = contact,Date = DateTime.Today,DueDate = DateTime.Today.AddDays(30),LineItems = lines
};
var invoiceList = new List<Invoice>();
invoiceList.Add(invoice);
var invoices = new Invoices();
invoices._Invoices = invoiceList;
var AccountingApi = new AccountingApi();
var response = await AccountingApi.CreateInvoicesAsync(accesstoken,xeroTenantId,invoices);
Requestempty _request = new Requestempty();
//trying this method to send email to specified invoice....
//var test = await AccountingApi.EmailInvoiceAsync(accesstoken,Guid.NewGuid(),null);
var updatedUTC = response._Invoices[0].UpdatedDateUTC;
return RedirectToAction("Index","InvoiceSync");
}
如何使用此方法将电子邮件发送到指定的发票..?
无法为隐式类型的变量分配空值,这使我出错。
此库中还有另一种方法。
var test = await AccountingApi.EmailInvoiceAsync(accesstoken,null);
我所使用的var test2 = await AccountingApi.EmailInvoiceAsyncWithHttpInfo(accesstoken,null);
仅用于测试,当我了解这两种方法的工作原理时,会添加创建的 GUID 。
更新1:
这是我使用的第二种方法。
Guid.NewGuid()
更新2:
这是我使用的代码。
await AccountingApi.EmailInvoiceAsyncWithHttpInfo(accesstoken,null)
解决方法
方法EmailInvoiceAsync
的返回类型似乎是Task
,返回类型为void。如果您等待任务,则没有可以分配给变量的返回类型。删除变量分配,并为类型RequestEmpty
的参数传递有效参数,以解决问题。
RequestEmpty requestEmpty = new RequestEmpty();
await AccountingApi.EmailInvoiceAsync(accessToken,xeroTenantId,Guid.NewGuid(),requestEmpty);
有关测试示例,请参见here
重要提示:根据documentation(请参见Emailing an invoice
),发票必须为ACCREC类型,并且必须具有有效的发送状态(SUMBITTED
,AUTHORISED
或PAID
)。